Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing floating numbers with [0, 1] from uniform distribution by using numpy

I'm currently trying to draw floating numbers from a uniform distribution.

The Numpy provides numpy.random.uniform.

import numpy as np
sample = np.random.uniform (0, 1, size = (N,) + (2,) + (2,) * K)

However, this module generates values over the half-open interval [0, 1).

How can I draw floating numbers with [0, 1] from a uniform distribution?

Thanks.

like image 970
siren99 Avatar asked Feb 17 '13 14:02

siren99


People also ask

How do you generate a random number from a uniform distribution in Python?

Use rand to generate 1000 random numbers from the uniform distribution on the interval (0,1). rng('default') % For reproducibility u = rand(1000,1);

How do you generate a random number from a uniform distribution?

To generate random numbers from the Uniform distribution we will use random. uniform() method of random module. In uniform distribution samples are uniformly distributed over the half-open interval [low, high) it includes low but excludes high interval.

What does uniform do in NumPy?

Draw samples from a uniform distribution. Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform .


3 Answers

It doesn't matter if you're drawing the uniformly distributed numbers from (0,1) or [0,1] or [0,1) or (0,1]. Because the probability of getting 0 or 1 is zero.

like image 60
Max Li Avatar answered Oct 15 '22 06:10

Max Li


random_integers generates integers on a closed interval. So, if you can recast the actual problem of yours into using integers, you're all set. Otherwise, you may consider if granularity of 1./MAX_INT is sufficient to your problem.

like image 29
ev-br Avatar answered Oct 15 '22 07:10

ev-br


From the standard Python random.uniform documentation :

The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (b-a) * random().

So basically, the inclusion of the end point is strictly based on the floating-point rounding scheme used. Therefore, to include 1.0, you need to define the precision required by your operation and round the random number accordingly. If you do not have a defined precision for your problem, you can use numpy.nextafter. Its usage was covered by a previous answer.

like image 26
CmdNtrf Avatar answered Oct 15 '22 06:10

CmdNtrf