68

This is the first time I'm trying random numbers with C (I miss C#). Here is my code:

int i, j = 0;
for(i = 0; i <= 10; i++) {
    j = rand();
    printf("j = %d\n", j);
}

with this code, I get the same sequence every time I run the code. But it generates different random sequences if I add srand(/*somevalue/*) before the for loop. Can anyone explain why?

Dan D.
  • 73,243
  • 15
  • 104
  • 123
Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112
  • 1
    Just as a side node: rand() implementations are usually pretty bad. This means short repetition cycles and low quality random numbers. So I'd use a third party PRNG and not the one included in your runtime. – CodesInChaos Oct 12 '10 at 20:09
  • [srand() — why call it only once?](http://stackoverflow.com/q/7343833/995714) – phuclv Apr 30 '17 at 09:58
  • See also [Recommended way to initialize `srand()`?](https://stackoverflow.com/q/322938/15168) – Jonathan Leffler Apr 22 '22 at 15:28

12 Answers12

112

You have to seed it. Seeding it with the time is a good idea:

srand()

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

int main ()
{
  srand ( time(NULL) );
  printf ("Random Number: %d\n", rand() %100);
  return 0;
}

You get the same sequence because rand() is automatically seeded with the a value of 1 if you do not call srand().

Edit

Due to comments

rand() will return a number between 0 and RAND_MAX (defined in the standard library). Using the modulo operator (%) gives the remainder of the division rand() / 100. This will force the random number to be within the range 0-99. For example, to get a random number in the range of 0-999 we would apply rand() % 1000.

Azeem
  • 11,148
  • 4
  • 27
  • 40
kjfletch
  • 5,394
  • 3
  • 32
  • 38
  • i already know this but my question is why does it give the same sequance when i dont use srand ? – Hannoun Yassir Jul 10 '09 at 10:29
  • 3
    Because if you don't seed it manually, it is ***ALWAYS*** seeded to 1 by default. See Aditya's answer. – GManNickG Jul 10 '09 at 10:41
  • 7
    If security is a concern, seeding it with the time is a rather bad idea, as an attacker can often find or guess the startup time relatively easily (within a few dozen to a few hundred attempts), then replay your sequence of pseudorandom numbers. If possible, try to make use of an operating-system-provided entropy source for your seed instead. – Dave Sherohman Jul 10 '09 at 10:46
  • 37
    If security is a concern, using rand() at all is a rather bad idea, no matter how you seed it. Aside from the unknown strength of the PRNG algorithm, it generally only takes 32 bits of seed, so brute forcing is plausible even if you don't make it extra-easy by seeding with the time. Seeding rand() with an entropy source for security is like giving a donkey steroids and entering it in the [Kentucky] Derby. – Steve Jessop Jul 10 '09 at 11:27
  • why is there a %100 after printf()? –  Oct 12 '10 at 09:43
  • 1
    "For example, to get a random number in the range of 0-999 we would apply rand()%1000" Be warned, the result is not evenly distributed unless 1000 divides evenly into RAND_MAX+1 (which it probably won't as RAND_MAX is often (2^n)-1), and there are a whole LOT of other problems too. See http://www.azillionmonkeys.com/qed/random.html – Boann Jun 21 '13 at 09:41
  • Great discussion here about why the need to seed http://superuser.com/questions/712551/why-are-people-so-bothered-about-truly-random-numbers-instead-of-ones-generated – Matthew Lock Feb 06 '14 at 03:26
  • How is it a "good idea"? If you run the program twice in less than a second, they will probably have the same seed. – asu Nov 06 '16 at 10:36
  • But again, Though you seed it with Time, it still will not be random but periodic(wrt to time) right? – AlphaGoku Feb 18 '19 at 06:09
40

rand() returns pseudo-random numbers. It generates numbers based on a given algorithm.

The starting point of that algorithm is always the same, so you'll see the same sequence generated for each invocation. This is handy when you need to verify the behavior and consistency of your program.

You can set the "seed" of the random generator with the srand function(only call srand once in a program) One common way to get different sequences from the rand() generator is to set the seed to the current time or the id of the process:

srand(time(NULL)); or srand(getpid()); at the start of the program.

Generating real randomness is very very hard for a computer, but for practical non-crypto related work, an algorithm that tries to evenly distribute the generated sequences works fine.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
nos
  • 223,662
  • 58
  • 417
  • 506
22

To quote from man rand :

The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.

If no seed value is provided, the rand() function is automatically seeded with a value of 1.

So, with no seed value, rand() assumes the seed as 1 (every time in your case) and with the same seed value, rand() will produce the same sequence of numbers.

Azeem
  • 11,148
  • 4
  • 27
  • 40
Aditya Sehgal
  • 2,867
  • 3
  • 27
  • 37
13

There's a lot of answers here, but no-one seems to have really explained why it is that rand() always generates the same sequence given the same seed - or even what the seed is really doing. So here goes.

The rand() function maintains an internal state. Conceptually, you could think of this as a global variable of some type called rand_state. Each time you call rand(), it does two things. It uses the existing state to calculate a new state, and it uses the new state to calculate a number to return to you:

state_t rand_state = INITIAL_STATE;

state_t calculate_next_state(state_t s);
int calculate_return_value(state_t s);

int rand(void)
{
    rand_state = calculate_next_state(rand_state);
    return calculate_return_value(rand_state);
}

Now you can see that each time you call rand(), it's going to make rand_state move one step along a pre-determined path. The random values you see are just based on where you are along that path, so they're going to follow a pre-determined sequence too.

Now here's where srand() comes in. It lets you jump to a different point on the path:

state_t generate_random_state(unsigned int seed);

void srand(unsigned int seed)
{
    rand_state = generate_random_state(seed);
}

The exact details of state_t, calculate_next_state(), calculate_return_value() and generate_random_state() can vary from platform to platform, but they're usually quite simple.

You can see from this that every time your program starts, rand_state is going to start off at INITIAL_STATE (which is equivalent to generate_random_state(1)) - which is why you always get the same sequence if you don't use srand().

caf
  • 233,326
  • 40
  • 323
  • 462
10

If I remember the quote from Knuth's seminal work "The Art of Computer Programming" at the beginning of the chapter on Random Number Generation, it goes like this:

"Anyone who attempts to generate random numbers by mathematical means is, technically speaking, in a state of sin".

Simply put, the stock random number generators are algorithms, mathematical and 100% predictable. This is actually a good thing in a lot of situations, where a repeatable sequence of "random" numbers is desirable - for example for certain statistical exercises, where you don't want the "wobble" in results that truly random data introduces thanks to clustering effects.

Although grabbing bits of "random" data from the computer's hardware is a popular second alternative, it's not truly random either - although the more complex the operating environment, the more possibilities for randomness - or at least unpredictability.

Truly random data generators tend to look to outside sources. Radioactive decay is a favorite, as is the behavior of quasars. Anything whose roots are in quantum effects is effectively random - much to Einstein's annoyance.

Tim H
  • 685
  • 3
  • 3
7

Random number generators are not actually random, they like most software is completely predictable. What rand does is create a different pseudo-random number each time it is called One which appears to be random. In order to use it properly you need to give it a different starting point.

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

int main ()
{
  /* initialize random seed: */
  srand ( time(NULL) );

  printf("random number %d\n",rand());
  printf("random number %d\n",rand());
  printf("random number %d\n",rand());
  printf("random number %d\n",rand());

  return 0;
}
Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
Howard May
  • 6,639
  • 9
  • 35
  • 47
2

This is from http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.13.html#rand:

Declaration:

void srand(unsigned int seed); 

This function seeds the random number generator used by the function rand. Seeding srand with the same seed will cause rand to return the same sequence of pseudo-random numbers. If srand is not called, rand acts as if srand(1) has been called.

Key
  • 6,986
  • 1
  • 23
  • 15
2

rand() returns the next (pseudo) random number in a series. What's happening is you have the same series each time its run (default '1'). To seed a new series, you have to call srand() before you start calling rand().

If you want something random every time, you might try:

srand (time (0));
Chet
  • 21,375
  • 10
  • 40
  • 58
1

Rand does not get you a random number. It gives you the next number in a sequence generated by a pseudorandom number generator. To get a different sequence every time you start your program, you have to seed the algorithm by calling srand.

A (very bad) way to do it is by passing it the current time:

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

int main() {
    srand(time(NULL));
    int i, j = 0;
    for(i = 0; i <= 10; i++) {
        j = rand();
        printf("j = %d\n", j);
    }
    return 0;
}

Why this is a bad way? Because a pseudorandom number generator is as good as its seed, and the seed must be unpredictable. That is why you may need a better source of entropy, like reading from /dev/urandom.

Josu Goñi
  • 1,178
  • 1
  • 10
  • 26
0

call srand(sameSeed) before calling rand(). More details here.

dfa
  • 114,442
  • 31
  • 189
  • 228
0

Seeding the rand()

void srand (unsigned int seed)

This function establishes seed as the seed for a new series of pseudo-random numbers. If you call rand before a seed has been established with srand, it uses the value 1 as a default seed.

To produce a different pseudo-random series each time your program is run, do srand (time (0))

nik
  • 13,254
  • 3
  • 41
  • 57
0

None of you guys are answering his question.

with this code i get the same sequance everytime the code but it generates random sequences if i add srand(/somevalue/) before the for loop . can someone explain why ?

From what my professor has told me, it is used if you want to make sure your code is running properly and to see if there is something wrong or if you can change something.

James
  • 17
  • 1
  • 3
    His question has already been answered before, if you read through the answers properly. What your professor told you is just a inference of why the rand() algorithm was programmed the intended way. – manav m-n Jan 31 '10 at 09:01