# [Tutor] What's the best way to model an unfair coin?

Evert Rol evert.rol at gmail.com
Sun Oct 24 13:54:16 CEST 2010

```> What's the best way to model an unfair coin?
>
> This is one way to do it, I suppose: Create a list containing only
> 'H's and 'T's. If you want the coin to have the probability of a head
> to be 6/11,
>
> ['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T']
>
> is the list to use. Use random.choice on the list, for a 6/11 heads
> probability.
>
> See <http://tutoree7.pastebin.com/gxKYkYWW>.
>
> That's the only way I can think of. But surely there's a better, more
> general solution. What if the probability I want is an irrational
> number, such as 1/e? Sure, I can calculate a fraction that's as close
> to that irrational number as I want, but..

My statistics might be too rusty to have this correct, but I would do something similar as you have now, just not for integer numbers.
Assuming you only want True or False, you can use a uniform distribution, through random.random(), and see if the result is lower or higher than your probability.
Eg:

return random.random() < 1/e

or

return random.random() < 6/11.

will return True or False with your specificied probability.
Again, I just might be overlooking something in the statistics.

Cheers,

Evert

Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e would be.

>
> Am I missing something that's already there in Python 2.6 or 3.1 (the
> 2 I have)?
>
> Dick Moores
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

```