unj2
unj2

Reputation: 53481

Do you have a better idea to simulate coin flip?

Right now i have

return 'Heads' if Math.random() < 0.5 

Is there a better way to do this?

Thanks

edit: please ignore the return value and "better" means exact 50-50 probability.

Upvotes: 1

Views: 5072

Answers (10)

William H. Hooper
William H. Hooper

Reputation: 655

There's a compact solution for bash in this github post:

(( RANDOM % 2 )) && {
    echo "heads" 
} || {
    echo "tails"
}

Upvotes: 0

DisgruntledGoat
DisgruntledGoat

Reputation: 72510

The only real answer to this question is that you cannot "guarantee" probability. If you think about it, a real coin flip is not guaranteed 50/50 probability, it depends on the coin, the person flipping it, and if the coin is dropped and rolls across the floor. ;)

The point is that it's "random enough". If you're simulating a coin flip then the code you posted is more than fine.

Upvotes: 0

garethm
garethm

Reputation: 2174

Numerical Recipes in C says not to trust the built in random number generators when it matters. You could probably implement the algorithm shown in the book as the function ran1(), which it claims passes all known statistical tests of randomness (in 1992) for less than around 108 calls.

The basic idea behind the ran1() algorithm is to add a shuffle to the output of the random number generator to reduce low order serial correlations. They use the Bays-Durham shuffle from section 3.2-3.3 in The Art of Computer Programming Volume 2, but I'd guess you could use the Fisher-Yates shuffle too.

If you need more random values than that, the same document also provides a generator (ran2) that should be good for at least 1017 values (my guess based on a period of 2.3 x 1018). The also provide a function (ran3) that uses a different method to generate random numbers, should linear congruential generators give you some sort of problem.

You can use any of these functions with your < 0.5 test to be more confident that you are getting a uniform distribution.

Upvotes: 7

DrStalker
DrStalker

Reputation: 9571

On a linux system you could read bits in from /dev/random to get "better" random data, but an almost random method like Math.Random() is going to be fine for almost every application you can think of, short of serious cryptography work.

Upvotes: 2

Bayard Randel
Bayard Randel

Reputation: 10086

a wee homage to xkcd:

string getHeadsOrTails {
        return "heads"; //chosen by fair coin toss,
                        //guaranteed to be random
    }

Upvotes: 6

David Cooper
David Cooper

Reputation: 61

I can't comment on people's posts because I don't have the reputation, but just an FYI about the whole <= vs. < topic addressed in Bill The Lizard's comment: Because it can be effectively assumed that random is generating any number between 0-1 (which isn't technically the case due to limitations on the size of a floating point number, but is more or less true in practice) there won't be a difference in num <= .5 or num < .5 because the probability of getting any one particular number in any continuous range is 0. IE: P(X=.5) = 0 when X = a random variable between 0 and 1.

Upvotes: 0

The Dissonant
The Dissonant

Reputation: 323

Try differentiating between odd and even numbers. Also, return an enumeration value (or a boolean), rather than a string.

Upvotes: 0

Bill the Lizard
Bill the Lizard

Reputation: 405695

What you have is the way I would do it. If 0.0 <= Math.random() < 1.0, as is standard, then (Math.random() < 0.5) is going to give you heads when Math.random() is between 0.0 and 0.4999..., and tails when it's between 0.5 and 0.999... That's as fair a coin flip as you can get.

Of course I'm assuming a good implementation of Math.random().

Upvotes: 4

jess
jess

Reputation: 926

there's always the dead simple

coin = rand(1);

in many scripting languages this will give you a random int between 0 and your arg, so passing 1 gives you 0 or 1 (heads or tails).

Upvotes: 10

Nick Berardi
Nick Berardi

Reputation: 54854

Try

return 'Heads' if Math.random() * 100 mod 2 = 0

I don't really know what language you are using but if the random number is dividable by two then it is heads if it is not then it is tails.

Upvotes: -4

Related Questions