This question arose to me while I was playing FIFA.
Assumingly, they programmed a complex function which includes all the factors like shooting skills, distance, shot power etc. to calculate the probability that the shot hits the target. How would they have programmed something that the goal happens according to that probability?
In other words, like a function X() has the probability that it return 1 89% and 0 11%. How would I program it so that it returns 1 (approximately) 89 times in 100 trials?
Generate a uniformly-distributed random number between
0and1, and returntrueif the number is less than the desired probability (0.89).For example, in IPython:
In this realisation, 8956 out of the 10000 boolean outcomes are
true. If we repeat the experiment, the number will vary around 8900.