True or false output based on a probability
问题 Is there a standard function for Python which outputs True or False probabilistically based on the input of a random number from 0 to 1? example of what I mean: def decision(probability): ...code goes here... return ...True or False... the above example if given an input of, say, 0.7 will return True with a 70% probability and false with a 30% probability 回答1: import random def decision(probability): return random.random() < probability 回答2: Given a function rand that returns a number between