How to change chance of heads or tails in a coin flip

I know this is the code for getting twice the number of heads and I’m trying different things out to rig a coin flip.

if (random(1)< (1/3)) {
    image (tail, mouseX, mouseY);
  }
 else {
  image (head,mouseX,mouseY);
 }

I was wondering how you would get 3 times as many heads as tails and 10% more tails than heads.

Hi ant,

First do you understand why in this case you get 2 times more head than tail?

If you pick a random number:

  • You have 1 chance over 1 to get a number lower than 1
  • You have 1 chance over 2 to get a number lower than 1/2
  • you have 1 chance over 3 to get a number lower than 1/3
  • you have 1 chance over n to get a number lower than 1/n

The reason is quite intuitive.

  • Take a segment and cut it in 3 for example.
  • Now if you pick a point at random on that segment you have 1 chance over 3 to be on the first section, 1 over 3 to be in the second and 1/3 to be in the last one.
  • The reason is that they all have the same size.

Now if your segment is of length 1, the three section need to be of length 1/3 to be all of the same size.

Thus, when you take a random number between 0 and 1 it has 1 chance over 3 to be in the first section so lower than 1/3.

So in your example you have:

  • 1 chance over 3 to get tail
  • 2 chances over 3 to get head (1 chance to be in the second section and 1 chance to be in the last section)

It means 2 times more head than tail.

Now imagine you want 3 times as many heads as tails. It means that for 1 tail you get 3 heads. You need 1 + 3 = 4 sections to accomplish that. Indeed, with 4 sections you have:

  • 1 chance over 4 to be in the first section
  • 3 chances over 4 to be in one of the 3 remaining ones

In code it would be like this:

if (random(1)< (1/4)) {
    image (tail, mouseX, mouseY);
  }
 else {
  image (head,mouseX,mouseY);
 }

For 10% more tails than heads, the principle is the same (it is just a bit more twisted). It is equivalent to say for 1 tail, I get 1.1 head. If you apply the same math as above your code will be:

if (random(1)< (1/(1+1.1))) {
    image (tail, mouseX, mouseY);
  }
 else {
  image (head,mouseX,mouseY);
 }
1 Like

Thank you, I wasn’t quite understanding the math part but your explanation cleared it up. Thank you!