0

In trying to generate a random number between 0.0 and 1.0, my program was generating pretty much the same 'random' number each time:

int main(){
    srand(time(NULL));
    double fraction = 1.0 / (RAND_MAX + 1.0);
    int number = rand();
    cout << "Random number: " << number * fraction << endl;

The output is always 0.698..., without fail. (Or at least in that region.)

I decided to see what would happen if I just repeat the process again within the same block:

int main(){
    srand(time(NULL));
    double fraction = 1.0 / (RAND_MAX + 1.0); 

    int number = rand();
    cout << "1st random number: " << number * fraction << endl;

    int number2 = rand();
    cout << "2nd random number: " << number2 * fraction << endl;

    cout << "\n";

This is the output of running this second block several times:

MacBook-Air-2:Worksheet2 Olly$ ./exercise5.out
1st random number: 0.700058
2nd random number: 0.870525

MacBook-Air-2:Worksheet2 Olly$ ./exercise5.out
1st random number: 0.700066
2nd random number: 0.00206314

MacBook-Air-2:Worksheet2 Olly$ ./exercise5.out
1st random number: 0.700073
2nd random number: 0.133601

MacBook-Air-2:Worksheet2 Olly$ ./exercise5.out
1st random number: 0.700073
2nd random number: 0.133601

MacBook-Air-2:Worksheet2 Olly$ ./exercise5.out
1st random number: 0.700081
2nd random number: 0.265139

As you can see, the second one is exhibiting behaviour that appears to be random, whereas the first one is remaining pretty much the same every time.

I suspect it is something to do with how my computer is compiling the code (maybe something has not been installed properly), per this discussion: http://www.cplusplus.com/forum/beginner/5131/. The discussion linked never actually came to a conclusion as to how to avoid this cross-machine difference in compiler-side behaviour.

The question: Why does "First random number" consistently exhibit much less fluctuation than "Second random number"?

quanty
  • 824
  • 1
  • 12
  • 21
  • 3
    the one occasion where you get both numbers exactly the same can be explained by `time` having an accuracy of seconds, ie if you run the program within the same second you should get exatly same random numbers. The others I dont really understand, but you shouldn't expect good quality randomness from `rand()` anyhow (see [rand considered harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful)). Use the facilities from `` if you want random numbers – 463035818_is_not_an_ai Apr 13 '20 at 11:48
  • btw the issue is not some "cross-machine difference in compiler-side behaviour", afaik all implementations of `rand()` are broken (maybe one more the other less, but who cares ;) – 463035818_is_not_an_ai Apr 13 '20 at 11:51
  • Does this answer your question? https://stackoverflow.com/questions/52869166/why-is-the-use-of-rand-considered-bad – 463035818_is_not_an_ai Apr 13 '20 at 11:53
  • fwiw, imho the accepted answer in the proposed dupe puts a little too much focus on the "pseudo" (but it correctly continues in stating that often what you need is exactly a pseudo-rng) – 463035818_is_not_an_ai Apr 13 '20 at 11:55
  • OK so clearly `rand()` is really bad, which thanks to you lot I am now aware of, and will stop using it! Still: @idclev463035818 thanks but that wasn't what I was asking about (I recognise that the seed would be the same for when I re-ran the code faster than 1s, and the question you posted helps explain why). The question you posted doesn't explain why I'm getting the behaviour of the first random number generated being the same every time (despite obviously different seed valued), whilst the second random number (with same seed value) is actually quite random. – quanty Apr 13 '20 at 12:29
  • I appreciate that I shouldn't use `rand()`, and will try this with `` instead, but I haven't seen anything that explains my seemingly strange output yet. – quanty Apr 13 '20 at 12:32
  • "but that wasn't what I was asking about" tbh then I dont understand what you are asking about. Anyhow, there was no response for a while and then I decided to flag as duplciate. In case there are any objections of course I'll happily vote to reopen... – 463035818_is_not_an_ai Apr 13 '20 at 12:36
  • @idclev463035818 it seems that you think I'm asking why two consecutive executions of the code would output the same result (the answer being that they were executed within 1s of each other, thus making the seed the same). I'm not asking that. I'm asking why, despite there being different seed values, each piece of code execution is outputting the same non-number in one instance (line 6 in the code), but a random number in the second instance (line 9). I don't see how that is at all the same as the duplicate. – quanty Apr 13 '20 at 13:38
  • "first random number generated being the same every time" ... but they're not. `0.700058` does not equal `0.700066`. Those two numbers are different. If they were the same, the "58" part and the "66" part wouldn't be different. Only `0.700073` is repeated twice, which may have to do with the resolution of your OS clock. – Eljay Apr 13 '20 at 13:41
  • no I understand that you ask why you get the output you get and why numbers are too similar. However, I do not understand why you say the numbers are the same when they are not. Moreover I dont understand what explanation you expect. I mean random numbers are random even if the randomness is of poor quality, actually poor quality of the rng might explain why you get similar result for similar seeds. It is a duplicate in the sense that what you observe can be explained (if it can be explained at all), by the low quality of `rand()` – 463035818_is_not_an_ai Apr 13 '20 at 14:17
  • fwiw, to observe a pattern you would need to take much more samples, not just 5. – 463035818_is_not_an_ai Apr 13 '20 at 14:19
  • btw from your comment: "I recognise that the seed would be the same for when I re-ran the code faster than 1s, and the question you posted helps explain why" No that is not what is the Q&A is about, it isnt even mentioned there. The accuracy of `time()` has nothing to do with `rand()` or `srand()`. You should read the Q&A again, because `rand()` has issues and they are not related to the 1s thing. I only mentioned that because it is evident in one instance in your output, but I never claimed that this is the answer for your question or that this would be the reason for proposing the duplciate – 463035818_is_not_an_ai Apr 13 '20 at 14:30
  • @idclev463035818 but in general I am not getting similar results for similar seeds, the issue is that I'm only getting similar results for similar seeds in the "1st random number" output. As you can see, the "2nd random number" output exhibits fluctuations, whereas "1st random number" output doesn't. I don't see why the two would have wildly different degrees of fluctuations when for each output they rely on the same seed. – quanty Apr 13 '20 at 15:48
  • "output exhibits fluctuations, whereas "1st random number" output doesn't" it is difficult to discuss when you use such sloppy language. There are fluctuations. Saying that there are none, does not help to clarify the issue. For the second part of your comment, basically what I said above, 5 samples are far too little to get something significant – 463035818_is_not_an_ai Apr 13 '20 at 16:06
  • I still dont understand what you are looking for. Let's say we could find an article that explains that under certain circumstances `rand()` will return numbers with little fluctuations on the first call. Then still the answer to your question would be "because `rand()` is broken". Anyhow, I am out, cheers ;) – 463035818_is_not_an_ai Apr 13 '20 at 16:09

0 Answers0