0

Hi I'm having trouble with my random number generator giving me the same sequence (sequence is different though) of numbers every time I run it. Here's a simplified tidbit of code to demonstrate my problem.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int random(){
    srand(time(0));
    return(1+rand() % 6);
}
int main(){
    cout<< random() <<endl;
    cout<< random() <<endl;
} // will return a repeated double digit i.e. 11, 22, 33, 44
user3743825
  • 115
  • 2
  • 3
  • 8
  • 2
    Same seed -> same sequence of numbers. Your program seeds it, then gets a number, then seeds it again (with the same seed), then gets the same number. – user253751 Mar 25 '15 at 03:55
  • 4
    Move this: `srand(time(0));` to the top of `main()`. Its a once per process kinda thing unless you're intentionally seeding (and by the looks of it, you're not). – WhozCraig Mar 25 '15 at 03:57
  • when I seed it again, doesn't the seed change because the time is different? – user3743825 Mar 25 '15 at 03:58
  • 1
    Seed only once, then generate all you want. On a side note, don't use `rand()` if C++11 is available to you. – Julian Mar 25 '15 at 03:58
  • 2
    The time isn't *that* different. On most systems `time()` is the number of seconds since the epoch, and a second doesn't tick by between your first and second calls. AndI concur with Atlas, use [``](http://en.cppreference.com/w/cpp/numeric/random). It really is the cat's pajamas if available to you. – WhozCraig Mar 25 '15 at 03:59
  • [Same random numbers every loop iteration](http://stackoverflow.com/q/9251117/995714) – phuclv Jul 30 '16 at 06:56

2 Answers2

3

rand() is 'pseudo-random' generator. That means it uses mathematical function to generate the next value from previous one.

srand() sets initial 'seed' value for subsequent calls of 'rand()'. If you seed same value you will get same random sequence. If you seed it with current time, sequence will be different each second.

Call it only once:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int random(){
    return(1+rand() % 6);
}

int main(){
    srand(time(0));

    cout<< random() <<endl;
    cout<< random() <<endl;
}
fukanchik
  • 2,811
  • 24
  • 29
1

srand() seeds the random generator. rand() is an algorithm which generates a random number based on the seed. Therefore you only need to call srand() once in your program. Read on for more...

And also, subsequent calls of rand() will automatically generate different numbers. e.g.

while (1){
    printf("%d ",rand()%100);
}

will print a list of numbers which are all different e.g. 5 34 65 23 56 ...

BEFORE you go implementing it, please note time(NULL) updates once per second. Instead, in int main(){ add srand(time(NULL)); once at the start.

Good luck!

acenturyandabit
  • 1,188
  • 10
  • 24
  • `NULL` is the same thing as `0`. – Julian Mar 25 '15 at 04:00
  • wait, really? well the rest of the advice applies. editing... – acenturyandabit Mar 25 '15 at 04:01
  • 3
    `In C++, the definition of NULL is 0, so there is only an aesthetic difference. ... Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer.` - [Bjarne Stroustrup](http://www.stroustrup.com/bs_faq2.html#null). – Julian Mar 25 '15 at 04:05