rand() problem

Apr 6, 2016 at 3:45pm
1
2
3
4
5
6
int main() 
{
srand(time(NULL));
int bunny_name_index = rand() % 100 + 0;
std::cout << bunny_name_index;
}


first run:
84
second run:
87
third run:
90

why is this happening?
the random numbers should be far from the previous one
Last edited on Apr 6, 2016 at 3:45pm
Apr 6, 2016 at 3:48pm
Why should random numbers be far from the previous one? If a random value was affected by a previous random value, it wouldn't be random.
Apr 6, 2016 at 3:49pm
so how do i do it?
Apr 6, 2016 at 3:50pm
No it shouldn't. Having to be far from the previous number would be a less-random requirement. Your sample size is 3, certainly not enough to judge the quality of the randomness.

(rand() isn't perfectly uniform though, but that is not the issue here)
Last edited on Apr 6, 2016 at 3:51pm
Apr 6, 2016 at 3:50pm
How do you do what? Generate random numbers? Or do you want to generate numbers that are far away from previous numbers (i.e. not random)?
Apr 6, 2016 at 3:52pm
i recorded 10 outputs:
first run:
92
second run:
93
third:
93
fourth:
96
fifth:
98:
sixth:
100:
seventh:
2
eigth:
5
nineth:
9
tenth:
10
Last edited on Apr 6, 2016 at 3:52pm
Apr 6, 2016 at 3:53pm
That's nice. Is there a question?
Apr 6, 2016 at 3:54pm
the random number i was expecting is like

23
94
57
1
76
42
Last edited on Apr 6, 2016 at 3:54pm
Apr 6, 2016 at 3:57pm
If you expected them, they aren't random. You don't understand what random means.

You think it means that each random number will be far away from the previous one. That's not true. That's not what random means. That's not how random numbers work.

It sounds to me like you don't want random numbers, but you want some numbers that are nicely spread out. Is that right?
Last edited on Apr 6, 2016 at 4:01pm
Apr 6, 2016 at 5:30pm
If the program is run several times in succession, the seed value won't have changed by much, and (depending on the implementation) the first few random numbers may not vary by much either.

If that's a problem, it's worth adding an extra call (or several) to rand()to generate and discard the first number after a call to srand().

Here, srand() is called with values differing by just 1, the first randoms are not very random. An extra call to rand() seems to help.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>


int main() 
{
    time_t seed = time(NULL);
    
    for (int i=0; i<10; i++)
    {
        srand (seed++);
        std::cout << std::setw(3) << rand() % 100;
    }    
    
    std::cout << "\n\n";
        
    for (int i=0; i<10; i++)
    {
        srand (seed++);
        rand();  // extra call to rand()
        std::cout << std::setw(3) << rand() % 100;
    }
    
}

My output:
 41 44 47 50 54 57 60 63 67 70

 61 42 90 38 19 67 16 96 44 93

Notice the first line of numbers just increase by a small amount in a fairly predictable fashion. The second set of numbers is less predictable.

edit: Note, this is for demonstration purposes only. Normally you would call srand() just once at the start. My program is an artificial example meant to simulate the effect of frequent re-running of a program.
Last edited on Apr 6, 2016 at 5:49pm
Apr 6, 2016 at 7:07pm
For completeness, no thread on C++ rand would be complete without a word from the STL himself:

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Topic archived. No new replies allowed.