Skip to content Skip to sidebar Skip to footer

How To Use Numpy.random To Generate Random Numbers From A Certain Distribution?

I am somewhat confused about how to use numpy.random to generate random values from a give distribution, say, binomial. I thought it would be import numpy as np np.random.binomial

Solution 1:

The first block of code uses a numpy.random.* function. numpy.random.* functions (including numpy.random.binomial) make use of a global RandomState object which is shared across the application.

The second block of code creates a pseudorandom generator object with default_rng() and uses that object to generate pseudorandom numbers without relying on global state.

Note that numpy.random.binomial (in addition to other numpy.random.* functions) is now a legacy function as of NumPy 1.17; NumPy 1.17 introduces a new pseudorandom number generation system, which is demonstrated in the second block of code in your question. It was the result of a proposal to change the RNG policy. The desire to avoid global state was one of the reasons for the change in this policy.

Solution 2:

import randomrandom.choice([2,44,55,66])

A crucial thing to understand about the random choice method is that Python doesn't care about the fundamental nature of the objects that are contained in that list.

Post a Comment for "How To Use Numpy.random To Generate Random Numbers From A Certain Distribution?"