I am working on a C++ assignment for my class, and we have to use rand () with a seed of 99 to produce a set of values. However, my problem is that when I try to create a value within our parameters, the number is different than what the instructor provided us for a definite first number. The code is shown below:
int lottoNumber;
srand (RANDOM_NUMBER_SEED);
do
{
lottoNumber = rand ();
} while (lottoNumber > 25 || lottoNumber < 1);
cout << lottoNumber << endl;
The value produced from this is 13, while the number expected to be produced is 2. Any help as to why this is different would be great, thanks!
You will notice that each time you start up a new MATLAB session, the random numbers returned by RAND are the same. This is because MATLAB's random number generator is initialized to the same state each time MATLAB starts up.
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.
In Golang, the rand. Seed() function is used to set a seed value to generate pseudo-random numbers. If the same seed value is used in every execution, then the same set of pseudo-random numbers is generated. In order to get a different set of pseudo-random numbers, we need to update the seed value.
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.
Algorithm used by rand()
is implementation-defined.
Which means that it might be different on different compilers and compiler versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With