-1
$\begingroup$

I was going to post this question on SO but I suspect it needs mathematical treatment. I need to make a decision(True or False) while running a simulation and I decided that this particular decision should be random.
At first I compared a random value with a static value (0.5) but I soon realized that I am getting 50% True, and 50% False. (So I can use 0.75 if I want 75% True and 25% False)
But I didn't want this particular distribution. So I wrote a function which compared a random value (between 0 and 1) with another random value.
[I have taken care of seeding the random generator with microseconds so IMO 'seed'ing part is not the problem.]
The function looks like

getResponse() {     rand1 = rand();     rand2 = rand();     if ( rand1 > rand2 )         return True;     else         return False; } 

My question is how random is the outcome of this function?
Here is the outcome (1=True 0=False) for 100 calls to function
0001101001010100110011100100011001010111101101100110010100100001110111110110001101101110000111010100
Do you see any pattern? Should there be any pattern? What happens if I reseed the random generator after say every 3rd call?
If this approach doesn't look right, can you suggest any other approach?


EDIT

I have realized my mistake of confusing pattern and distribution. I apologize for the same. I must understand first what distribution I want . Unless that is taken care of, I can not make any progress.
After going through comments and suggestions, I feel that a functional approach will either require keeping track of past results or decide on the distribution of outcome.
On second thoughts I decided neither is necessary if we get a constantly changing variable into the picture.
Enter microtime() which returns microseconds after current second.
Too bad I can't answer till next 8 hours, so I am excusing myself to type it here.

getResponse() {     return ( microtime()*1000000 % 2 ) ? True : False; } 
  • 0
    thanks for the clarification, I have realized my mistake, I think I confused distribution with pattern, and I must go back to my simulation as problem lies there, not in this function. After evaluating the needs, I should decide on the distribution. Sorry for this mess :( . Thank you all for your help. I hope to report back here (if I use the answer given below) with my findings.2011-08-25

1 Answers 1

1

Use http://www.random.org/clients/ or
http://en.wikipedia.org/wiki/Hardware_random_number_generator.

When there have been m falses and n trues,
return false with probability (n+1)/(m+1+n+1) and true with probability (m+1)/(m+1+n+1).

  • 0
    Thats a very good point @Steven . Thank you very much for your insight and explanation. I must admit my lack of knowledge of what _randomness means_ (as I expressed in my last update of question) and if one thing this exercise has taught me, that for every function that returns a bounded value, there will be _some_ (overall) distribution in hiding. And it is this outcome that I must look/think for before getting into what function I should use. I thank you all again for your help.2011-08-25