Search code examples
pythonrandom

How to generate random numbers with specific probabilities in python?


For example,

Try to generate a list of 1000 random numbers with only 0 and 1.

s.t.

0 has the prob of 40%

1 has the prob of 60%

thanks!


Solution

  • myProb = []

    for i in range(1000):
    #creates one number out of 0 or 1 with prob p 0.4 for 0 and 0.6 for 1
        test = numpy.random.choice(numpy.arange(0, 2), p=[0.4, 0.6])
        myProb.append(test)
    

    print(myProb)