0

An efficient way to create random numbers in C/C++ is rand() function. but I've seen code like this to create random variables:

int x; x%=100;

Is this a good way to produce a simple random number? If your answer is no, please tell me why?

EDIT : well the actual code is here:

int temp1,temp2;
A=(abs(temp1))%11-1;
B=(abs(temp2))%11-1; //Randomize without using rand()

A friend of mine wrote this code. I tried to compile it and I got uninitialized local variable 'temp1' used error (on MSVS). He wrote this code in 2011 and it worked on his Linux with latest version of GCC.

MoNo
  • 307
  • 4
  • 15
  • 9
    This is terrible. It relies on whatever garbage values were left by the previous use, and in debug mode might very well be 0 all the time (some debuggers 0 initialize ints). That being said, [`rand()` is terrible too](http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful). Just use ``. – Borgleader Oct 22 '14 at 16:48
  • why don't you try printing out the value in a loop where the optimization is turned on. – AndersK Oct 22 '14 at 16:48
  • 2
    This is fine because the C runtime populates all uninitialized memory with random numbers sourced from atmospheric noise before running your program. Or maybe not. – James M Oct 22 '14 at 16:49
  • this may just as much not even compile on any half decent compiler, or at least will generate warnings saying that `uninitialized variable x` – Creris Oct 22 '14 at 16:49
  • 5
    I don't think the question is terrible. The idea suggested in the question is. Not sure it deserves downvotes. – Bathsheba Oct 22 '14 at 16:51
  • Relevant http://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior-in-c – 2501 Oct 22 '14 at 16:58
  • 2
    +1 Indeed it's an excellent question. I for one am glad that the OP is asking it rather than just quietly using this code (and potentially leaking it into software that I use!) without question. – Lightness Races in Orbit Oct 22 '14 at 17:18
  • `(abs(temp1))%11-1;` may generate values `-1` to `9` or some other negative value like `-3`: `(abs(INT_MIN))%11-1`. – chux - Reinstate Monica Oct 22 '14 at 17:43

2 Answers2

6

It's rare to see something worse.

You have undefined behaviour as you are using an uninitialised variable.

And using modulus introduces statistical bias.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
4

Your friend has misunderstood uninitialised variables.

Ignoring for a moment that reading from them is undefined and can thus do just about anything, if you pretend that they will safely yield an arbitrary value then you need to remember that arbitrary does not mean random.

This approach has somewhere between minimal and no random distribution of values. So you won't be able to predict what you get back, but that does not make it usefully "random" in any meaningful sense.

Also, applying % ruins any distribution so don't do that either.

Tell your friend to turn warnings on in his compilation settings. His GCC is trying to tell him all this, but he isn't listening.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055