简体   繁体   中英

Query on rand() function in C++

I have 2 queries on rand() function usage in C++:

  1. Where is rand() function defined ? I wrote a simple program to cout<<rand()<<endl; in a loop and I didnt include any header file except <iostream> .. How did this work? In the reference examples I came across in a few sites, some said, you need to include <stdlib.h> , others said, <time.h> .. So wondering how my program worked..Any ideas?
  2. I had heard before using "rand()", one needs to initialize by giving a seed to srand, and typically the seed is current unix time -> srand(time(NULL)) .. But again, I didnt do this in my simple program which just had cout<<rand()<<endl; in a while loop and it was displaying random numbers.. So question: is srand(time(NULL)) used to improve randomness, as it's not mandatory? if not what could be the point of using it.

Appreciate your help!

Thanks!

It is declared in <cstdlib> header. Standard library headers ( <iostream> in you example) may include other standard headers, but you should not rely on that as it is implementation specific. Include the headers you need explicitly.

Seeding the random number generator is mandatory, unless you're happy with the fact that your program produces the same "random" sequence every time you run it :)

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

srand (time(NULL));

initializing the seed must be done if you want rand() to produce (different) pseudorandom sequence each time you run your program

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

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

As for seeding, if you don't seed the generator yourself, you'll get a default seed, which will be the same every time you run the program, as if you called srand(1) . You'll get the same sequence of numbers from rand() every time you run the program. That's useful for debugging, but, of course, isn't useful when the application is out there in the real world. To produce a different sequence of random numbers, call srand when your program starts, and give it a different value each time you call it. That's what stand(time(NULL)) does.

i will address the questions:

1)the rand() function is defined in stdlib as you can read here . the time is referenced for time(NULL) for srand function

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

For every different seed value used in a call to srand , the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.

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

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

In order to generate random-like numbers, srand is usually initialized to some distinctive runtime value, like the value returned by function time (declared in header <ctime> ). This is distinctive enough for most trivial randomization needs.

  1. Its include in cstdlib

    This standard header might have been include by you iostream

  2. Did you re-run your program and notices anything common between generated numbers that from previous run.

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

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

Where is rand() function defined?

It is included in c standard library <stdlib.h> , while <cstdlib> is a just c++ wrapper of <stdlib.h> . If you need time() function, do remember to include <time.h> , too.

is srand(time(NULL)) used to improve randomness, as it's not mandatory?

It is not mandatory in most cases. Since the rand() do not generate truly random number, but pseudo random number, in which the generating process depends on a seed, or an initial value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM