Sign up ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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;
        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
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?


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;
share|cite|improve this question
The probability of getting True is still 1/2. –  Florian Aug 25 '11 at 7:13
What distribution DO you want? –  Ricky Demer Aug 25 '11 at 7:16
So, the question should be: Does the quality improve if one compares two random numbers instead of comparing one random number to 1/2. I can't think of any reason why this should be true, but I can think of one why the quality can get worse: the cycle length decreases to one half of the original one. –  Florian Aug 25 '11 at 7:31
@Sudhi - what do you mean by random then? I'm unclear what you are trying to achieve. –  Mark Bennet Aug 25 '11 at 7:43
Maybe you want the probability of getting True to be fixed the start of each simulation. So, at the start, somehow determine a number p, and getResponse becomes { return rand() <= p; } Is this what you want? I'm just guessing wildly. –  Florian Aug 25 '11 at 8:41

1 Answer 1

up vote 1 down vote accepted

Use or

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).

share|cite|improve this answer
thanks for your answer, it does seem a good option but I am not so keen on it as it entails keeping track of past results, which I don't want. How about this (see my answer below) –  Sudhi Aug 25 '11 at 9:53
darn! cant answer, here is the short of it. microtime() returns time in microseconds after the current second. So getResponse() { return ( microtime()*1000000 % 2 ); }. What do you think about it? –  Sudhi Aug 25 '11 at 10:00
That would not be "(truly) randomly". –  Ricky Demer Aug 25 '11 at 10:02
can you please elaborate on it? –  Sudhi Aug 25 '11 at 10:09
Also, using a RNG like 'time mod 2' is a dangerously bad idea. Most notably, many systems will not return those sorts of values with as much precision as you'd expect; I've seen multiple systems where the 'time in microseconds' value is always a multiple of 2 - in fact, always a multiple of 10! I strongly discourage using anything like this, and encourage some further study of what randomness means, because I think you're still somewhat confused on the core principles. –  Steven Stadnicki Aug 25 '11 at 17:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.