Loupax
Loupax

Reputation: 4914

Is this way of generating random numbers safe and valid?

I was doing some recreational low-level programming the other day, and somewhere in my code this thing emerged (using pseudocode):

cointoss program

char out = '0';

void thread(){ out = '1';}

int main(){
   // Creates a thread that will run the function we provided
   // The main() will not wait for this thread to finish
   runInOwnThread(thread);

   printr("%d", out);
   return 0;
}

Now in our shell we run cointoss program x number of times and we will end up with something like this:

0011011100001000

which we can convert to decimal and get a random number

My question is not whether this is an efficient way of creating a random number but more about we can tell if the end result is random enough for use

Upvotes: 1

Views: 32

Answers (1)

mattm
mattm

Reputation: 5949

No, using a race condition is not a good way to generate random numbers. The result will be nondeterministic, but the result is likely to be heavily weighted towards one outcome.

If you don't care about the values being heavily weighted (that is random enough for you), then go ahead.

Upvotes: 2

Related Questions