Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using garbage values to generate random or Pseudo-random numbers

I come across this idea of using garbage values to generate random numbers. In C the variable that is not initialised is given a garbage value(but when I try it in my geany it gives a 0). So if want to take a random number from 50-60. Can I take the modulus of garbage values and then add them to the initial limit of the random number. But uninitialised variable returns zero instead of garbage value as in this post @ How garbage values are assigned to variables in c

int i;
int LowerLimit=50;
int UpperLimit=60;
int RandomNumber = i%(UpperLimit-LowerLimit) + LowerLimit;

Also what if we use a hundred uninitialised variables to use a combination of them to generate a random variable.

like image 985
xtreak Avatar asked Jan 18 '26 05:01

xtreak


2 Answers

Using uninitialized variables as random values is a very bad idea in practice. Using uninitialized values is Undefined Behavior so everything could happen, but in practice a local uninitialized variable may hold some well repeatable value. Some compilers are clever enough, when optimizing, to remove some uses of uninitialized values (or on the contrary to set that value as a constant). This is conforming to C or C++ standards.

Recent C++11 standard has defined a quite extensive API for random numbers. See the <random> C++11 standard header.

I mean that if you print some uninitialized value, you may get in practice always the same value when running the same program several times on the same machine, and I don't call that a random behavior. Of course the behavior is implementation specific (and will vary from one system to the next; it is dependent on the operating-system, compiler, optimization flags, and user configuration or environment, etc...).

I suggest using some pseudo-random number generator (e.g. see random(3) or lrand48(3)...) and seeding it with some random input. On Linux you could read some few bytes from /dev/urandom (read carefully urandom(4) ...) or combine the current time with the current pid (see time(2) and getpid(2)) to obtain a seed.

And random numbers does not mean much, and randomness is difficult to define precisely in practice (ask a mathematician, specialist in probabilities).

Random numbers is really a difficult subject. You can spend your entire life on it, and get a PhD....

For debugging purposes you may want to have a reproducible stream of pseudo-random numbers. To get that, use a well defined seed.

If your problem depends crucially on having a really random and unpredictable stream of numbers (e.g. if you are coding a poker web site), it is worth spending a few hundred euros (or dollars) buying some hardware random device.

like image 86
Basile Starynkevitch Avatar answered Jan 20 '26 17:01

Basile Starynkevitch


An uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. Which means that it can have 100 stored in the variable even if you generate a random number a 1000 times and maybe on the 1001th time you get some other number. This is not a good idea. When something is undefined we can just not have any clue what will happen, and generating random numbers needs a proper algorithm, its not something a programmer is clueless about.

This is non-standard. I believe SO expects standard coding.

like image 41
Sadique Avatar answered Jan 20 '26 17:01

Sadique