-7

I don't understand the meaning of srand() in <time.h> to create a random number. Here is my code:

/* srand example */
#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main ()
{
  printf ("First number: %d\n", rand()%100);
  srand (time(NULL));
  printf ("Random number: %d\n", rand()%100);
  srand (1);
  printf ("Again the first number: %d\n", rand()%100);

  return 0;
}

and the results are:

First number: 41
Random number: 13
Again the first number: 41

Why is the result of srand(1) different from srand(2)? Why does the result of srand(1) or srand(2) keep appearing again and again? And why do I have to use srand(time(NULL)) in order to create a dynamic random number?

  • read this: http://www.cplusplus.com/reference/cstdlib/srand/ – Eugene Sh. Feb 03 '15 at 17:28
  • 5
    Why is this tagged c and c++? In c++, `rand()` should not be used at all, it is deprecated and [considered harmful](http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful). – leemes Feb 03 '15 at 17:29
  • @leemes `rand()` is neither deprecated nor considered harmful in C++. It definitely has its restrictions, but that has been true since the early days of C. – James Kanze Feb 03 '15 at 17:55

5 Answers5

5

If you look at the docs:

Seeds the pseudo-random number generator used by std::rand() with the value seed.

rand() has some internal state that it keeps from one call to the next. The function is deterministic - but we can view its output as pseudorandom. So the value produced by:

srand(1);
rand();

will always be the same for a given implementation. That's why the notes state that:

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.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
Barry
  • 286,269
  • 29
  • 621
  • 977
1

srand() uses a seed to determine what the number will be. srand() always produces the same number when given the same seed. If you don't give it a seed, then it uses something in the system to determine what the seed will be, and this makes the numbers appear random - the seed is constantly changing. srand(1) will always be the same.

FCo
  • 485
  • 5
  • 17
1

The function rand() (and most other “random” sources1) is an implementation of a “pseudo-random number generator”. The numbers it generates are not random at all, but simply numbers in a very long sequence of discreet values; the sequence is designed so that successive numbers appear random, according to some suitable definition of random.

The function srand() simply sets a starting point in this sequence.

[1] Most OS do have some source of truly random numbers, such as the file /dev/random on Unix systems. They can be very slow for more than a few bytes, however. There main use is to seed a PRNG.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

What's usually called a random number generator is actually a pseudo-random number generator. This typically means that you can generate the same random sequence if you provide the "key" to that sequence, referred to as the "seed". This is very useful when you wish to test your algorithm that is based on randomization, and you need to ensure repeatable results.

If you do not "seed" your Random number generator, it is seeded with 1

Seed values are integers that define the exact sequence of pseudo-random numbers, but there's no way of knowing ahead of time what sequence it will be and there's no way of tweaking a sequence by slightly changing the seed. Even the tiniest change in seed value will result in a radically different random sequence.

Sridhar Nagarajan
  • 1,085
  • 6
  • 14
  • Your second paragraph doesn't apply to the standard library `srand()/rand()`. If you don't call `srand()` before `rand()` it's documented to behave as though `srand(1)` was called. – Blastfurnace Feb 03 '15 at 17:37
0

Hmm, you need better understanding of how Pseudo-Random Number Generators (PRNGs) work. The word "Pseudo" is very important: actually it is very hard to generate really random number, it is easier to take it once (like time in seconds) then compute other values based on it.

I.e. Linear Congruential Generator which is often used for libc rand() calculates random number based on previous value, so:

formula from wiki

First X is set by srand()

myaut
  • 11,174
  • 2
  • 30
  • 62