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

**URL:** <https://discourse.processing.org/t/how-to-change-chance-of-heads-or-tails-in-a-coin-flip/3531>\
**Category:** Coding Questions\
**Created:** [September 14, 2018, 12:18pm UTC](https://discourse.processing.org/t/how-to-change-chance-of-heads-or-tails-in-a-coin-flip/3531 "2018-09-14T12:18:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [September 14, 2018, 12:18pm UTC](https://discourse.processing.org/t/how-to-change-chance-of-heads-or-tails-in-a-coin-flip/3531/1 "2018-09-14T12:18:24Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 14, 2018, 12:42pm UTC](https://discourse.processing.org/t/how-to-change-chance-of-heads-or-tails-in-a-coin-flip/3531/2 "2018-09-14T12:42:41Z")

</div>

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:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [September 14, 2018, 12:46pm UTC](https://discourse.processing.org/t/how-to-change-chance-of-heads-or-tails-in-a-coin-flip/3531/3 "2018-09-14T12:46:20Z")

</div>

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