0

I like to learn by screwing around with code, recently I copied and pasted a random number generator code. Then I removed all the lines of code that were not "necessary" to make the executable work to generate a random number. The final straw was me deleting "time" from srand.

srand((unsigned) time(0));

What is the point of "time(0)" here?

Does it use the time that the program is opened to generate the seed for the random number? Is that why removing it (time) makes it not work? Because then it doesn't have a seed?

Also...

include <stdlib.h> 
include <stdio.h> 
include <time.h>

int main() 
{

srand((unsigned) time(0));

printf("Your dice has been rolled! You got:");

int result = 1 + (rand() % 20);

printf("%d", result);

}

that's the whole code and I noticed it used the "rand" result for output. Does the "rand" pull the seed from "srand"?

  • 2
    You should be able to figure it out by yourself: simply run your program a couple of times, with and without srand, and then see if you can figure out the answer by yourself. It should be doable. When you see the results the answer should be very obvious. – Sam Varshavchik Jan 19 '21 at 01:13
  • 1
    The seed you pass to srand doesn't have to be time(0) - it could be any value that is different every time srand is called. The current time is different every time, but you could also ask the user to type in a random number (assuming you trust them to be random) or you could count the number of lines in a log file (assuming you trust it to be always changing) or something else. The easiest thing that doesn't require any work on your part is the current time, which is why time(0) is often used. – Jerry Jeremiah Jan 19 '21 at 01:18
  • [man 2 time](https://man7.org/linux/man-pages/man2/time.2.html) ??? – David C. Rankin Jan 19 '21 at 01:25
  • 1
    Just here to point out that rand() is random enough for fun, but you might need something better for serious work. – John Bayko Jan 19 '21 at 02:07

2 Answers2

2

If you don’t “seed” the random number generator (or if you use the same seed value), you’ll get the same set of pseudorandom numbers.

Using the current time is an easy way to get a different seed every time.

Buddy
  • 10,874
  • 5
  • 41
  • 58
0

The effect of srand cannot cross threads, so the random number seed should be set once on each thread. @Buddy said that using time(0) is the most convenient way to do this, and each call will get a different seed.Of course you can use an atomic variable .

std::atomic<int> seek(2374213);  //init whatever you like
  
void thread1fun()
{
  srand(++seek);
  //...
  int rand_num = rand();
}

void thread2fun()
{
  srand(++seek);
  //...
  int rand_num = rand();
}
Lichard
  • 41
  • 5
  • `so the random number seed should be set once on each thread` Any references? [CppRef](https://en.cppreference.com/w/c/numeric/random/srand) says `Generally speaking, the pseudo-random number generator should only be seeded once, before any calls to rand(), and the start of the program. It should not be repeatedly seeded, or reseeded every time you wish to generate a new batch of pseudo-random numbers.`. – Sprite Jan 19 '21 at 02:27
  • I found a ref [here](https://stackoverflow.com/a/10159747/13433323), but its only for Windows. – Sprite Jan 19 '21 at 02:29
  • @Sprite — it’s not for Windows, it’s for Microsoft’s compiler. – Pete Becker Jan 19 '21 at 04:01