Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ rand () not providing correct number based on seed

Tags:

c++

random

srand

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!

like image 935
V. Anderson Avatar asked Feb 11 '17 00:02

V. Anderson


People also ask

Why is rand () giving me the same number?

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.

Why does Rand give me the same number C++?

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.

What is rand () seed?

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.

Does rand () give 0?

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

Algorithm used by rand() is implementation-defined.

Which means that it might be different on different compilers and compiler versions.

like image 53
HolyBlackCat Avatar answered Oct 04 '22 00:10

HolyBlackCat