-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomShuffle.cpp
61 lines (48 loc) · 1.21 KB
/
RandomShuffle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* \file RandomShuffle.cpp
* \brief
*
* Returns a random number which is is chosen by shuffling the elements of a vector which
* only contains values that exist between the specified range
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
// Generates a random number between specified min/max boundaries using a vector to shuffle.
int
random2(int nMin, int nMax)
{
std::vector<int> vi;
static int nIndex = 0;
for (int i = nMin; i < nMax; i ++) {
vi.push_back(i);
}
std::random_shuffle(vi.begin(), vi.end());
return vi[nIndex];
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
int i = 0;
std::cout << std::endl;
std::cout << "Using Random2 with std::vector shuffling" << std::endl << std::endl;
for (i = 0; i < 10; i ++) {
std::cout << random2(1, 100) << std::endl;
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
Using Random2 with vector shuffling
97
19
65
67
67
47
19
90
1
91
#endif