0

Possible Duplicate:
why do i always get the same sequence of random numbers with rand()?

I am confounded by the fact that even using different programs (on the same machine) to run /compile, and after nilling the vaues (before and after) the function.. that NO MATTER WHAT.. I'll keep getting the SAME "random" numbers… each and every time I run it. I swear this is NOT how it's supposed to work.. I'm going to illustrate as simply as is possible…

#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {

    int rPrimitive = 0;  rPrimitive = 1 + rand() % 50;
    NSNumber *rObject = nil; rObject = [NSNumber numberWithInt:rand() % 10];
    NSLog(@"%i  %@", rPrimitive, rObject);

    rPrimitive = 0;   rObject = nil;
    NSLog(@"%i  %@", rPrimitive, rObject);
    return 0;           
}

Run it in TextMate:

i686-apple-darwin11-llvm-gcc-4.2
8  9
0  (null)

Run it in CodeRunner:

i686-apple-darwin11-llvm-gcc-4.2
8  9
0  (null)

Run it a million times, if you'd like. You can gues what it will always be. Why does this happen? Why oh why is this "how it is"?

Community
  • 1
  • 1
Alex Gray
  • 16,007
  • 9
  • 96
  • 118
  • My question really had more to do wih the fact that these results span across logical divides of the OS / memory and that they occur despite assigning null to the values, than to `rand`, itself. – Alex Gray Apr 12 '12 at 23:50
  • 2
    @alexgray That just means that the different OSs are using the same PRNG, which isn't unexpected. Setting nulls doesn't change anything, since this is the expected behavior of `rand` rather than a memory bug. – Aaron Dufour Apr 13 '12 at 04:52
  • Don't remove the auto-generated duplicate link, please. You can flag this question for moderator attention, or raise the issue on [Meta.SE] if you believe it should be re-opened. – jscs Apr 29 '12 at 20:54

3 Answers3

6

This is why (from the rand man page):

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

Since it is always seeded with the same number it will always produce the same sequence of numbers. To get it to produce a different sequence each time it runs, you need to use a different seed each time it runs. You can use srand() to set the seed.

QuantumMechanic
  • 13,795
  • 4
  • 45
  • 66
3

Because the numbers aren't random, they're pseudorandom. They're generated according to an algorithm which will always produce the same output, given the same initial seed. You're not seeding the PRNG, so it uses a default, constant seed.

If you seed the PRNG using something less predictable (such as the current time and/or PID), you will get different results each time. In the case of rand(3), you need to seed it with srand(3).

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
2

The reason it is like that is because rand is a pseudo-random-number-generator, meaning it doesn't generate true random numbers (which is actually a very difficult thing to do). It generates the next number in the sequence using the “seed”, and at the start of execution the seed is always set to the same value (1 or so), so if you don't change the seed, you'll always get the same sequence of random numbers. You can use something like srand(time(NULL)); to seed the random number generator based on the time, or you can use a random number generator that is considered strong enough for cryptographic purposes, arc4random.

You might thing “why is it like this?”, but there are some cases where you want to generate the same series of “random numbers” multiple times.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • Ahh, yes, I had previously used arc4random, which is why this seemed so odd.. Indeed placing `#define rand() (arc4random() % ((unsigned)RAND_MAX + 1))` before main brought sense to the universe, again. Thanks. – Alex Gray Apr 12 '12 at 23:45
  • 1
    In a deterministic machine (aka a computer), "generating" true random numbers is impossible, not difficult. You need an outside source of randomness. – Aaron Dufour Apr 13 '12 at 04:54
  • @alexgray even by doing this, you are still using pseudo-random numbers, it just happens that arc4random seeds itself with something more unique than the constant value `1` – tobyodavies Apr 13 '12 at 05:26
  • @AaronDufour: OpenBSD's "outside sources" include mouse movements, network activity, etc. It's still not truly random of course but it is random enough for almost all practicable purposes. Also, it's not impossible, it is difficult. See [here](http://www.random.org/randomness/). – dreamlax Apr 13 '12 at 06:48
  • @dreamlax None of those involve "generating" randomness. They use outside sources, which I pointed out as the only way to get true random numbers. – Aaron Dufour Apr 13 '12 at 14:37
  • @AaronDufour: I'm not saying they *are* generating randomness, I'm saying they're *generating random numbers* based on outside randomness. It's a *random number generator* not a *randomness generator outputting numbers*. – dreamlax Apr 13 '12 at 20:51