Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does rand() yield the same sequence of numbers on every run?

Tags:

c++

random

Every time I run a program with rand() it gives me the same results.

Example:

#include <iostream> #include <cstdlib>  using namespace std;  int random (int low, int high) {     if (low > high)         return high;     return low + (rand() % (high - low + 1)); }  int main (int argc, char* argv []) {     for (int i = 0; i < 5; i++)         cout << random (2, 5) << endl; } 

Output:

3 5 4 2 3 

Each time I run the program it outputs the same numbers every time. Is there a way around this?

like image 822
Patrick Lorio Avatar asked Feb 27 '12 01:02

Patrick Lorio


People also ask

Why does rand keep giving me the same number?

The RAND function in stand-alone applications generates the same numbers each time you run your application because the uniform random number generator that RAND uses is initialized to same state when the application is loaded.

Can rand return same number?

The rand() function in C++ is used to generate random numbers; it will generate the same number every time we run the program. In order to seed the rand() function, srand(unsigned int seed) is used. The srand() function sets the initial point for generating the pseudo-random numbers.

Can you generate the same random numbers everytime?

random seed() example to generate the same random number every time. If you want to generate the same number every time, you need to pass the same seed value before calling any other random module function. Let's see how to set seed in Python pseudo-random number generator.

What does the function rand () do?

Description. RAND returns an evenly distributed random real number greater than or equal to 0 and less than 1. A new random real number is returned every time the worksheet is calculated.


1 Answers

The seed for the random number generator is not set.

If you call srand((unsigned int)time(NULL)) then you will get more random results:

#include <iostream> #include <cstdlib> #include <ctime> using namespace std;  int main() {     srand((unsigned int)time(NULL));     cout << rand() << endl;     return 0; } 

The reason is that a random number generated from the rand() function isn't actually random. It simply is a transformation. Wikipedia gives a better explanation of the meaning of pseudorandom number generator: deterministic random bit generator. Every time you call rand() it takes the seed and/or the last random number(s) generated (the C standard doesn't specify the algorithm used, though C++11 has facilities for specifying some popular algorithms), runs a mathematical operation on those numbers, and returns the result. So if the seed state is the same each time (as it is if you don't call srand with a truly random number), then you will always get the same 'random' numbers out.

If you want to know more, you can read the following:

http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/

http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/

like image 171
4 revs, 3 users 90% Avatar answered Sep 23 '22 01:09

4 revs, 3 users 90%