Select an action with probability p [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this questionWhat does it mean to say "do this with probability p"? Does it mean that if p > 0.5, we will do "this"?
How would you write the code or algorithm for doing something with the probability p?
Thank you,
No, it's more or less: choose a random number between 0 and 1 then, if that's is less than or equal to p
, do something.
For example, say p
is equal to 0.75 (do something with a probability of 75%). When selecting random numbers in the range 0 through 1, about 75% of them will be 0.75 or less.
In terms of programming, you could code this up as (pseudo-code, obviously):
def do (action, probability):
if rand() <= probability:
action
精彩评论