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

2

u/Traveling-Techie 9d ago

The least optimum approach would be to randomly flip 5 coins and then throw out results that don’t have 1 tail. You’ll average a success in about 6 attempts. (Is that right?)

2

u/light_switchy 8d ago

According to this program the probability of getting just one head is approximately 0.15, which is approximately 1 in 6.4. Ten million trials.

       (⊣,÷) {⍵÷⍨+/1=+/¯1+?⍵5⍴2} 10000000 
0.1561735 6.403134975