r/AskComputerScience 2d ago

How to program weighted random system

I want to make a program where you are controlling the outcomes. For an example with a coin flipping game, I want exactly 4 Heads and 1 Tail, but I want their positions to be random.

I do not want to use the random algorithms that library I want to build my own.

Right now I am learning probability from Youtube and might look at khan academy and coursera, but how can I implement this into programming I do not want to just take courses or watch videos

How do I go about that?

EDIT: I am doing this because I want to start build gambling algorithms and I am starting with a simple project like coin flips

3 Upvotes

15 comments sorted by

View all comments

1

u/donaldhobson 1d ago

Keep track of the number of heads and tails remaining.

Generate a random number r uniform between 0 and 1.

If you have 1 head and 3 tails left, then if r<1/(1+3)=1/4, output heads, otherwise, output tails.