1

I read that rand() generates the same number when not seeded. So, I added srand() and i fed it with unity spaced integers.

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

int main()
{
    int i;

    for(i = 0 ; i < 10 ; i++)
    {
        srand(i);
        printf("%d ",rand());
    }
    return 0;
}

But the result was predictable.

38 41 45 48 51 54 58 61 64 68

The results had a difference of 3 or 4. I tried the same for the interval -9 to 0. The results were predictable again..!

9 12 15 19 22 25 28 32 35 38

Why is it that rand() generates such numbers..?

Why does srand(0) always lead to 38 and srand(1) always lead to 41 and so on..?

Sagar
  • 404
  • 3
  • 14
  • 1
    You are only supposed to seed it once. – Havenard Oct 05 '18 at 02:41
  • 1
    Because you shouldn't call `srand` within the loop.... – David C. Rankin Oct 05 '18 at 02:41
  • 2
    @MitchWheat: `rand` is implementation defined. Why would you expect your results to match OP’s? And, no, you are not “supposed” to call `srand` only one. You are supposed to call `srand` to seed a new sequence. OP wished to examine multiple sequences, so they used multiple seeds. – Eric Postpischil Oct 05 '18 at 02:45
  • Because those are the first numbers your implementation of `rand()` produces from those seeds. – Shawn Oct 05 '18 at 02:49
  • 2
    @DavidC.Rankin: Using `srand` within the loop was necessary and appropriate for OP’s stated goal: Examining the behavior of `srand` in response to `rand`. This code is not their production code for using `rand`; it is their exploratory code for examining the behavior of `rand` and `srand`. Furthermore, the fact that they called `srand` repeatedly is **not** the cause of the generated numbers incrementing by three. That is a property of the particular `srand` and `rand` implementation, revealing that it does little processing with the seed. – Eric Postpischil Oct 05 '18 at 02:58
  • @ Eric Postpischil : you know what I meant. – Mitch Wheat Oct 05 '18 at 05:01
  • "38 41 45 48 51 54 58 61 64 68" looks a shade more random than [9 9 9 9 9 9](http://ardiri.com/blog/secure_number_generator_for_the_arduino) – chux - Reinstate Monica Oct 05 '18 at 06:52
  • @Sagar Devanand What compiler are you using? – chux - Reinstate Monica Oct 05 '18 at 06:54
  • 1
    @EricPostpischil -- it is *exactly* the cause of the numbers incrementing by three *because of the property of the particular* `srand`. The *semi-random* distribution is apparently generated with the first term incremented by `3`. Re-initializing the distribution and printing the first value returned each trip through the loop is producing the values seen exactly because there is little processing of the seed. None of which is contrary to what I wrote in my comment. – David C. Rankin Oct 05 '18 at 07:38
  • Possible duplicate of [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – Joseph Sible-Reinstate Monica Oct 08 '18 at 01:43

2 Answers2

2

There is no such thing as true randomness in computer systems, the rand() function will create a pseudo-random number based on some mathematical formulas that use a seeded initial value and then the state left after each subsequent rand() call.

You only have to seed it once (only call srand() once in the entire program).

That being said, rand() will generate the same sequence of results based on a given seed. If you give it the same seed every time, it will generate the same sequence every time.

For it to work properly, you need to use a different seed every time you run the program. Using time() as seed is a popular choice.

Havenard
  • 27,022
  • 5
  • 36
  • 62
  • 1
    Re “There is no such thing as true randomness in computer systems”: Some processors, including mainstream Intel processors, collect entropy from thermal noise and/or other sources specifically to make it available for random number generation, and some software collects noise from environmental inputs, for the same reason. – Eric Postpischil Oct 05 '18 at 03:01
  • @EricPostpischil I know, you can use sensors as source for your seed, including a microphone to capture ambience noise (I believe random.org claims to use that method). But technically it's still not random, it's data from a source. – Havenard Oct 05 '18 at 03:05
  • 2
    The physics community awaits your paper demonstrating the universe is in fact deterministic. – Eric Postpischil Oct 05 '18 at 03:06
  • @EricPostpischil It's complex but certainly not random, everything happens for a reason, or thousands of them. – Havenard Oct 05 '18 at 03:07
  • 3
    @EricPostpischil The majority view amongst physicists currently is that the universe is deterministic. But that is not particularly relevant to this question – M.M Oct 05 '18 at 03:19
  • 1
    @M.M: The lack of any random chance is [superdeterminism](https://en.wikipedia.org/wiki/Superdeterminism), and it is not the prevailing model in physics. Probabilistic models are much more favored. – Eric Postpischil Oct 05 '18 at 10:58
1

rand is notorious for being not very well implemented in some C implementations. It is conceivable the generator does very little work with the seed to produce the next value of rand().

You should look at the second number of each sequence and see if they have a similar pattern. If so, the implementation may be using a linear congruential generator.

You might want to avoid using the built-in srand and rand and look for alternatives such as Unix’ srandom and random.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    A good, fast (but not cryptographically-secure) PRNG with a C reference implementation that’s easy to find is xorshift+. – Davislor Oct 05 '18 at 04:47