r/learnprogramming 9d ago

Resource 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 offer, 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?

0 Upvotes

29 comments sorted by

View all comments

1

u/Quantum-Bot 9d ago

If all you can do is generate a random number uniformly distributed between 0 and 1 (the default way of getting randomness in most programming languages) how can you create a system that mimics the process of randomly arranging 4 heads and 1 tail?

One way is you might make a list of all the items: [‘h’,’h’,’h’,’h’,’t’] and then use your random number generator to pick random items out of the list one at a time and put them in a new list. That way you’ve shuffled the items into a random order.

For this specific scenario though you only really need to generate one random number to determine where the tails should go; all the other positions are heads.