Request for rand () function in C ++

I have 2 requests to use the rand () function in C ++:

  • Where is the rand () function defined? I wrote a simple program for cout<<rand()<<endl; in a loop, and I did not include any header file except <iostream> .. How does it work? In reference examples I, some said that you need to include <stdlib.h> , others said: <time.h> .. It is so interesting how my program worked. Any ideas?
  • I heard before using rand (), you need to initialize by providing a seed for srand, and usually the seed is the current time unix β†’ srand(time(NULL)) . But then again, I did not do this in my simple program, which simply had cout<<rand()<<endl; in a while loop, and it displayed random numbers. So the question is: srand(time(NULL)) used to improve randomness, since this is not necessary? if not, then what's the point of using it.

Appreciate your help!

Thanks!

+6
source share
7 answers

Declared in the <cstdlib> header. The standard library headers ( <iostream> in your example) may include other standard headers, but you should not rely on this because it is implementation specific. Include the headers you need explicitly.

Filling out the random number generator is mandatory, unless you are satisfied that your program creates the same "random" sequence every time it starts :)

+3
source

rand() function is declared in stdlib.h, so you need #include <stdlib.h> in your program. You may also want to #include time.h because the function time() declared there, which is used to initialize random seeds with

 srand (time(NULL)); 

seed initialization must be done if you want rand() execute a (different) pseudo-random sequence every time you run your program

http://en.cppreference.com/w/cpp/numeric/random/rand

+1
source

rand() is defined in the standard library. It is declared in the headers <stdlib.h> and <cstdlib> ; in the first, in the global namespace; in the second, in the std . You must #include appropriate headers for all the standard library elements that your program uses; sometimes names will be declared in other headers, but this is purely internal to your implementation.

As for sowing, if you do not sow the generator yourself, you will get a default seed that will be the same every time you start the program, as if you called srand(1) . You will get the same sequence of numbers from rand() every time you run the program. This is useful for debugging, but certainly not useful when the application is in the real world. To create another sequence of random numbers, call srand when your program starts, and give it a different value each time it is called. What stand(time(NULL)) does.

+1
source

I will consider the following issues:

1) the rand() ) function is defined in stdlib , as you can read here . time refers to time(NULL) to the srand function

2) srand() - The pseudo random number generator is initialized using the argument passed as a seed.

For every other seed value used in the srand call, you can expect the pseudo random number generator to generate a different sequence of results on subsequent calls to rand.

Two different initializations with the same seed will generate the same sequence of results in subsequent calls to rand.

If the seed value is 1, the generator is reinitialized to its initial value and returns the same values ​​as before any rand or srand call.

To generate random numbers, srand usually initialized with some distinctive runtime value, such as the value returned by the function time (declared in the <ctime> header). This is common enough for most of the trivial needs of randomization.

0
source
  • Include it in cstdlib

    This standard header may be included by iostream

  • Re-launched his program and noticed something in common between the generated numbers, which were from the previous launch.

    Yes !, so srand is what you need, it will generate a random number generator with the current time.

0
source

If you read the manual page - http://linux.die.net/man/3/rand - it will tell you that it is automatically sown with a value of one. As for why you do not need to include stdlib , this should be included in the bowels of iostream

0
source

Where is the rand () function defined?

It is included in the standard library c <stdlib.h> , and <cstdlib> is just a C ++ shell <stdlib.h> . If you need the time() function, be sure to include <time.h> .

is srand (time (NULL)) used to improve randomness as it is not necessary?

In most cases this is not necessary. Since rand() does not generate a truly random number, but a pseudo-random number in which the generation process depends on the seed or the initial value.

So, if you need a generated serial number different from the last, you need to set a different seed, otherwise each execution of your program will generate the same number.

0
source

Source: https://habr.com/ru/post/952450/


All Articles