Questions tagged [coin-flipping]

Coin flipping is the process of programmatically simulating a coin flip, that is randomly picking a choice among two possibilities.

coin-flipping
Filter by
Sorted by
Tagged with
36 votes
7 answers
53k views

How do I simulate flip of biased coin?

In unbiased coin flip H or T occurs 50% of times. But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'. something like this: def flip(p): '''this ...
Pratik Deoghare's user avatar
7 votes
11 answers
83k views

Python code for the coin toss issues

I've been writing a program in python that simulates 100 coin tosses and gives the total number of tosses. The problem is that I also want to print the total number of heads and tails. Here's my code:...
Ru1138's user avatar
  • 163
7 votes
3 answers
1k views

Coin flip simulation never exceeding a streak of 15 heads

I was working on a simulation of the St Petersburg Paradox when I realized my coin-flipping code never recorded any streaks of greater than 15 heads in a row. I ran the simulation 100,000,000 times, ...
tpixel's user avatar
  • 97
4 votes
4 answers
2k views

Count number of alternations in a coin flip sequence

I have a sequence of ones and zeros and I would like to count the number of alternations, e.g: x <- rbinom(10, 1, 1/2) > x [1] 0 0 1 1 1 1 1 0 1 0 Thus I would like to count (in R) how many ...
Frank Zafka's user avatar
4 votes
3 answers
2k views

Simulate coin toss for one week?

This is not homework. I am interested in setting up a simulation of a coin toss in R. I would like to run the simulation for a week. Is there a function in R that will allow me to start and stop the ...
Frank Zafka's user avatar
4 votes
2 answers
5k views

Coin flip program

I tried making a program that flips a coin(shows image of heads first and later shows image of tails) and I encountered problems trying to have the image of the coin viewed when I ran the problem; ...
user1629075's user avatar
4 votes
1 answer
2k views

3D Coin Flipping Animation on SKSpriteNode

I'm currently developing a game using Swift 3 and SpriteKit. I have a coin that falls during a game that the user can collect. Right now, it falls and doesn't have any rotation or anything. I want to ...
Nik's user avatar
  • 1,664
3 votes
4 answers
15k views

Flip a coin problem

I have been playing around and wrote this little piece of code. I am trying to flip a coin defined number of times and then count how many tails and heads I am getting. So here it is: private void ...
HelpNeeder's user avatar
  • 6,420
3 votes
11 answers
20k views

Automate the boring stuff - Coin flip streaks

I know there's tons of questions about it by now, even for the same problem, but I think I tried a bit of a different approach. The task is to to 10.000 samples of 100 flips each and then compute ...
mr_harm's user avatar
  • 61
3 votes
4 answers
7k views

Tossing a fair coin for 100 times and count the number of heads. Repeat this simulation 10**5 times

Write a program to simulate tossing a fair coin for 100 times and count the number of heads. Repeat this simulation 10**5 times to obtain a distribution of the head count. I wrote below code to count ...
Hashmatullah Noorzai's user avatar
3 votes
3 answers
724 views

Random Number Generator / Coin Flip / A-B Generator - Run Until X in a Row in R

I am trying to generate random results, such as from a coin flip. I'd like to run this A/B or heads/tails generator until I get x of the same result consecutively. I found this code online for a ...
QuantStudentR's user avatar
3 votes
3 answers
4k views

Nested loops with C++ coin toss

I have to write a program that runs a loop for a coin toss. I am supported to enter a number into the console and have it run a loop of the coin toss for that many times. I need to use nested loops. I ...
David's user avatar
  • 397
3 votes
3 answers
5k views

Coin Flip Simulation

I just started to learn Python and am trying to code the following question: Coin Flip Simulation- Write some code that simulates flipping a single coin however many times the user decides. The code ...
Danqi Liu's user avatar
2 votes
2 answers
1k views

How to program an unfair or biased coin flip in Ruby?

I need to make a coin flip that obeys a certain probability of outcome. For example, a coin flip with a 67% chance of coming out Heads, 83% chance of coming out Tails, etc. I managed to get the ...
San Diago's user avatar
  • 1,050
2 votes
1 answer
564 views

Generating random uniform random numbers from coin flip algorithm tends to generate more 0s than expected

I am trying to generate random numbers in the range from 0 to 99 using a function rcoin that returns 0 or 1 with equal probability. I have written the following code that converts a binary number ...
Prakash Dutta's user avatar
2 votes
1 answer
951 views

coin flip experiment in C++ using std::rand() giving incorrect result

I'm trying to count the number of occurrences of the string HHHHHHTTTTTT in 1 million flips. For the coin flip, I'm using a simple std::rand() % 2. The code below. Mathematically, the expected answer ...
xdavidliu's user avatar
  • 2,771
2 votes
1 answer
167 views

Simulating 10,000 Coinflips in Python Very Slow

I am writing a simulation that creates 10,000 periods of 25 sets, with each set consisting of 48 coin tosses. Something in this code is making it run very slowly. It has been running for at least 20 ...
mks212's user avatar
  • 921
2 votes
1 answer
283 views

Comparing multiple coin flip experiments. 1 Experiment = 100 Flips. Trying to simulate 10 experiments

import random as rd n = 0 ListOfStreaks = [] ListOfResults = [] while n != 10: numberOfStreaks = 0 for i in range(100): Flip = rd.randint(0,1) ListOfResults.append(Flip) ...
Trey's user avatar
  • 21
2 votes
1 answer
929 views

php loop flip coin until 2 heads, then stop

I'm new to php and am trying to write a loop that will flip a coin until exactly two heads have been flipped and then stop. So far I've written a function for coin flipping: function cointoss () { ...
pzstein's user avatar
  • 35
2 votes
2 answers
359 views

Coin Flip Game function

I am trying to create a function in python where a player starts with a certain amount and then given a certain stake and number of flips it will give the ending amount after the flips I am a ...
Rito Lowe's user avatar
2 votes
2 answers
188 views

Dealing with normal (Gaussian) distribution

I basically stucked on rather simple problem: Toss N coins and discover, how many of them land heads Solution performance must not depend on N, so we can't just call Math.random() < 0.5 N times....
augur's user avatar
  • 236
2 votes
1 answer
1k views

How do I write a function that prints counts of values in an array, taking the array as a parameter?

Starting with the below program that creates an array of coin flips, I have to add a function that prints out the total number of heads and total number of tails flipped. The function must take the ...
J..'s user avatar
  • 35
2 votes
1 answer
3k views

2 Player Coin Flipping

I'm trying to create a Java program that flips two coins to see who wins the coin flip. It prompts the user for a number of coin flips. If the first player has heads and the second player has tails, ...
user1762387's user avatar
2 votes
0 answers
235 views

Unbiase coin toss using the C++11 random library

I have a program that needs to generate very many 0/1 random values. Because this is needed many times and in different part of the code, I wish to have a time efficient and easy to use random ...
Prolix's user avatar
  • 284
1 vote
9 answers
5k views

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.
unj2's user avatar
  • 52.8k
1 vote
3 answers
2k views

Java Coin Flip Suggestions

I'm working on a coin flip assignment and I have the majority of it functioning properly (albeit in a less elegant fashion compared to the code I see on here). I'm trying to find a way to tell the ...
Zack's user avatar
  • 671
1 vote
2 answers
265 views

Implementing a bias in coin toss simulation

I want to simulate a biased coin being flipped 2048 times and record the results from each attempt, along with the total count of each coin. My current code is fully functional and is able to simulate ...
Sebastian Algharaballi's user avatar
1 vote
2 answers
416 views

Function to output probability in PHP

I'm trying to find a function that takes a number and outputs the expected results as follows: if input=1 then output should be Array{'0','1'} if input=2 then output should be Array{'00','01','10','...
Songo's user avatar
  • 5,688
1 vote
3 answers
19k views

How flipping a coin and display tails and heads counting?

I am very beginner on Java, still getting passion about. I have been given this exercise: "Write a simulator program that flips a coin: One thousand times then prints out how many time you get tails ...
Fred882401's user avatar
1 vote
2 answers
2k views

convert an unfair coin into a fair coin in Python 2.7

Using Python 2.7. Suppose I have an unfair coin and I want to turn it into a fair coin using the following way, Probability of generating head is equal for unfair coin; Flip unfair coin and only ...
Lin Ma's user avatar
  • 9,979
1 vote
2 answers
4k views

C++ Calling function inside another function

In my C++ class we've been given an assignment to make a coin toss program that has the random number generator in one function and it is called into another function that runs it twelve(12) times. ...
user3427139's user avatar
1 vote
3 answers
3k views

How to return all combinations of N coin flips using recursion?

Request: Using JavaScript, write a function that takes an integer. The integer represents the number of times a coin is flipped. Using recursive strategies only, return an array containing all ...
cj102's user avatar
  • 13
1 vote
2 answers
331 views

These Directions Are Not Clear To Me

I have to make a program that uses two files called coin. One file named coin and the other named coin tester. The directions in the book are confusing to me, but I still tried to do the assignment. ...
Lucas Kopp's user avatar
1 vote
2 answers
202 views

Generating Coin flips using purrr

Now I'm learning how to use purrr package in r, and thinking about how to generate 5 samples of each 1, 2, ..., 99, 100 coin flips. My image is to create a list, that should look like.. [[1]] [[1]] [...
恵比寿了's user avatar
1 vote
1 answer
616 views

Coin Toss Plot similar to Feller

From Feller (1950) An Introduction to Probability Theory: A path of length n can be interpreted as the record of an ideal experiment consisting of n successive tosses of a coin. If +1 stands for ...
Frank Zafka's user avatar
1 vote
2 answers
474 views

Create a function in R for simulating a coin flip game

In a coin flip game, you flip a fair coin until the difference between the number of heads and number of tails is 3. You are paid $8 at the end, but you have to pay $1 for each flip of the coins. Use ...
user avatar
1 vote
1 answer
3k views

Python coin flip with functions

I need to create a python program that will use various functions to simulate flipping a coin 100 times and finding the largest streak of "H"'s out of 10,000 tries. I am stuck on how to ...
Rebecca Krouse's user avatar
1 vote
2 answers
111 views

Coin tossing simulation unexpected probabilities

This is a script I wrote to simulate a coin toss game that ends in a given fixed sequence of outcomes (tosses are either ones or zeroes). This fixed sequence characterizes the game. For example, ...
Psmith's user avatar
  • 43
1 vote
1 answer
1k views

Generate a random number using coin flips while guarenteeing termination

The usual method to generate a uniform random number 0..n using coin flips is to build a rng for the smallest power of two greater than n in the obvious way, then whenever this algorithm generates a ...
owencm's user avatar
  • 8,574
1 vote
1 answer
106 views

Question About a Probability Model Using C++

I am trying to solve a probability question theoretically and then modeling it on C++ but my code outputs a different probability from the theoretical one. Question: A balanced coin is tossed three ...
Mohamed Soliman's user avatar
1 vote
1 answer
65 views

How to find the stability of a series of binary sequence

I am currently working on a project where I need to find the stability of multiple binary sequences of same length. samples: [1,1,1,1,1,1] and [0,0,0,0,0,0] are stable [1,0,0,1,1,0] is comparatively ...
Shyam's user avatar
  • 89
1 vote
1 answer
193 views

Generate data from a coin flipping experiment

I have a coin and the coin has memory. i.e. the last flip impacts the probability of heads on this flip. How do I write code to generate data from this process? Specifically, from this process, I want ...
Gingerbread's user avatar
  • 2,042
1 vote
1 answer
136 views

Interpretation of the coin toss code in C

During classes the teacher presented the C code for simulation of the coin toss. I know that there have been answered many questions about this topic, however, none of them relates strictly to my ...
Question's user avatar
1 vote
1 answer
327 views

How does modulo work when simulating a coin toss in c++?

#include <iostream> #include <stdlib.h> #include <ctime> int main() { // Create a number that's 0 or 1 srand (time(NULL)); int coin = rand() % 3; // If number is ...
JJL's user avatar
  • 71
1 vote
1 answer
161 views

Displaying multiple images via setInterval very slow in browsers

I'm trying to simulate a coin flip with javascript. It keeps changing images with 4 positions, creating an impression of flipping for 5 seconds and then sets the source to either the "head" image or "...
Tiko  Harutyunyan's user avatar
1 vote
1 answer
202 views

Iteration performance

I made a function to evaluate the following problem experimentally, taken from a A Primer for the Mathematics of Financial Engineering. Problem: Let X be the number of times you must flip a fair ...
Josmoor98's user avatar
  • 1,791
1 vote
3 answers
2k views

python - infinite coin flip that stops when number of heads = number of tails

I'm new to python and I'm trying to create a coinflip loop which will keep flipping and counting the number of flips until the number of heads = the number of tails, where it will stop and print the ...
Mei Lian Hoe's user avatar
1 vote
1 answer
221 views

Code to count times to get all heads or all tails is returning only times to get heads

I am pretty happy to say I think I have my first coin flip problem running. I am counting how many times it takes to get all heads or all tails in a trial of 7 flips. In theory it should take around ...
CPTxShamrock's user avatar
1 vote
0 answers
47 views

Modelling three biased coins from generated data

Here is an experiment I ran to better understand pymc3. I have three biased coins and I perform the below experiment: 1. Toss Coin1. If head choose Coin2 else choose Coin3 2. Randomly choose a number ...
ScalaSparkPlay's user avatar
1 vote
1 answer
669 views

Compute the posterior probability given a Bernoulli distributed likelihood

In a coin flip, we would like to compute p(theta|Data), where theta is the underlying parameter. The prior follows a beta distribution with parameters a and b. The likelihood follows a Bernoulli ...
Salma Bouzid's user avatar