Coin flipping - returning random outcome

NightShade

forgive me if this was already posted :) I have had a problem stuck in my head all day which I have been trying to think of an efficient solution for. Basically the problem is this: Imagine you have to flip a coin 3 billion times and would like a function that returns the amount of heads after all these flips. One possible solution would be to obviously make a for loop iterating 3 billion times recording how many heads and tails and returning the heads - this is obviously an incredibly inefficient solution. I thought of binomial probability but could not see where this could come in to help solve this problem (i'm probably missing something really obvious).

For example say I input in the function NumberOfHeads(flips) most the time (statistically) it will likely return some number around flips / 2. However say flips = 3 billion there should still be a chance that (although incredibly slim to never) it may return 1000 heads or something. Hope I explained whats been troubling me well enough :) thanks for any reponses.

Brad Solomon

You could use scipy.stats.binom here. The function below returns the number of heads from a randomly sampled binomial distribution with fair coin flips at each (bernoulli) trial.

import scipy.stats as scs
def num_heads(num_flips):
    flips = scs.binom(n=num_flips, p=0.5)
    return np.asscalar(flips.rvs(1))

num_heads(3000000000)
# 1499985766

.rvs() here stands for random variate.

Without looking through the source, I'm going to guess random number generation is using the analytic binomial CDF p=CDF(x), taking the inverse CDF, then choosing p from a ~U(0,1) distribution. You can read more about that method in Downey - ThinkStats - section 5.6. Disclosure: I might be totally wrong, as I often am.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Biased coin flipping?

From Dev

solution for the flipping coin issue

From Dev

Tallying the outcome of a coin flip

From Dev

Javascript Image Replace Coin Flipping

From Dev

3D Coin Flipping Animation on SKSpriteNode

From Dev

How flipping a coin and display tails and heads counting?

From Dev

Coin Flipping/Card picking program not working

From Dev

Using a button to display the outcome of a coin flip Javascript

From Dev

Create a binary outcome with random forest

From Java

Flipping a coin and stopping when it lands heads 4 times in a row

From Dev

Better than brute force algorithms for a coin-flipping game

From Dev

Better than brute force algorithms for a coin-flipping game

From Dev

Postgres returning empty result if one of the outcome is null

From Dev

Comparison of two identical NSDates returning unequal outcome

From Dev

Random cropping and flipping in convolutional neural networks

From Dev

Tasks in C# - unclear outcome using Random

From Dev

r generate random binary outcome with given probability

From Dev

Randrange outcome help? Python Random Number Generator

From Dev

ViewFlipper: flipping at random time intervals using random children

From Dev

How to calculate what is probability of getting same result 8 times in a row, when flipping coin 1000 times?

From Dev

Sendto returning random values

From Dev

Random generator returning endless duplicates

From Dev

Returning random generator from runST

From Dev

Creating, Displaying and Returning Random Numbers

From Dev

Returning a Random Position and Color With SASS

From Dev

fscanf returning random large numbers

From Dev

toString method is returning random characters

From Dev

How would I prevent a random function from choosing the last outcome of that function when run another time?

From Dev

Flipping the UILabel

Related Related

  1. 1

    Biased coin flipping?

  2. 2

    solution for the flipping coin issue

  3. 3

    Tallying the outcome of a coin flip

  4. 4

    Javascript Image Replace Coin Flipping

  5. 5

    3D Coin Flipping Animation on SKSpriteNode

  6. 6

    How flipping a coin and display tails and heads counting?

  7. 7

    Coin Flipping/Card picking program not working

  8. 8

    Using a button to display the outcome of a coin flip Javascript

  9. 9

    Create a binary outcome with random forest

  10. 10

    Flipping a coin and stopping when it lands heads 4 times in a row

  11. 11

    Better than brute force algorithms for a coin-flipping game

  12. 12

    Better than brute force algorithms for a coin-flipping game

  13. 13

    Postgres returning empty result if one of the outcome is null

  14. 14

    Comparison of two identical NSDates returning unequal outcome

  15. 15

    Random cropping and flipping in convolutional neural networks

  16. 16

    Tasks in C# - unclear outcome using Random

  17. 17

    r generate random binary outcome with given probability

  18. 18

    Randrange outcome help? Python Random Number Generator

  19. 19

    ViewFlipper: flipping at random time intervals using random children

  20. 20

    How to calculate what is probability of getting same result 8 times in a row, when flipping coin 1000 times?

  21. 21

    Sendto returning random values

  22. 22

    Random generator returning endless duplicates

  23. 23

    Returning random generator from runST

  24. 24

    Creating, Displaying and Returning Random Numbers

  25. 25

    Returning a Random Position and Color With SASS

  26. 26

    fscanf returning random large numbers

  27. 27

    toString method is returning random characters

  28. 28

    How would I prevent a random function from choosing the last outcome of that function when run another time?

  29. 29

    Flipping the UILabel

HotTag

Archive