I want to generate random numbers in the range -1, 1
and want each one to have equal probability of being generated. I.e. I don't want the extremes to be less likely to come up. What is the best way of doing this?
So far, I have used:
2 * numpy.random.rand() - 1
and also:
2 * numpy.random.random_sample() - 1
NumPy random for generating an array of random numbers 10 000 calls, and even though each call takes longer, you obtain a numpy. ndarray of 1000 random numbers. The reason why NumPy is fast when used right is that its arrays are extremely efficient. They are like C arrays instead of Python lists.
random. choice() function is used to get random elements from a NumPy array. It is a built-in function in the NumPy package of python.
Numpy uses the Mersenne Twister (as does the cpython random module). When you seed the random number generator you're choosing its current state (a PRNG chooses its next state based on its current state and chooses its current value as a function of its current state.
Your approach is fine. An alternative is to use the function numpy.random.uniform()
:
>>> numpy.random.uniform(-1, 1, size=10) array([-0.92592953, -0.6045348 , -0.52860837, 0.00321798, 0.16050848, -0.50421058, 0.06754615, 0.46329675, -0.40952318, 0.49804386])
Regarding the probability for the extremes: If it would be idealised, continuous random numbers, the probability to get one of the extremes would be 0. Since floating point numbers are a discretisation of the continuous real numbers, in realitiy there is some positive probability to get some of the extremes. This is some form of discretisation error, and it is almost certain that this error will be dwarved by other errors in your simulation. Stop worrying!
Note that numpy.random.rand
allows to generate multiple samples from a uniform distribution at one call:
>>> np.random.rand(5) array([ 0.69093485, 0.24590705, 0.02013208, 0.06921124, 0.73329277])
It also allows to generate samples in a given shape:
>>> np.random.rand(3,2) array([[ 0.14022471, 0.96360618], [ 0.37601032, 0.25528411], [ 0.49313049, 0.94909878]])
As You said, uniformly distributed random numbers between [-1, 1) can be generated with:
>>> 2 * np.random.rand(5) - 1 array([ 0.86704088, -0.65406928, -0.02814943, 0.74080741, -0.14416581])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With