Vladimir
Vladimir

Reputation: 225

How to generate random numbers close to 1 using Numpy

I want to generate a numpy array of random numbers close to 1. Is there a quick way to do so that allows me to set desired neighborhood from 1, say 1e-5?

Upvotes: 0

Views: 327

Answers (3)

Engineero
Engineero

Reputation: 12918

If you want, for example, 1000 uniform random numbers in the range [1 - 1e-5, 1 + 1e-5):

nums = np.random.uniform(low=1-1e-5, high=1+1e-5, size=1000)

Upvotes: 1

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95948

Use a uniform distribution:

>>> window = 1e-5
>>> np.random.uniform(low=1-window, high=1+window, size=10)
array([ 1.00000539,  0.99999055,  1.00000759,  0.99999228,  1.00000737,
        1.00000557,  1.00000522,  1.00000375,  1.00000054,  0.99999047])

Upvotes: 0

fferri
fferri

Reputation: 18940

Check the numpy.random module:

For example, normally distributed numbers with mean of 1.0 and standard deviation of 0.002:

>>> numpy.random.normal(1, 0.002, (5,))
array([1.00246167, 0.99722898, 0.99793482, 1.00100399, 1.00004228])

Using uniform distribution:

>>> numpy.random.uniform(1-1e-5, 1+1e-5, (5,))
array([1.00000668, 1.00000037, 0.99999398, 0.99999736, 1.00000645])

Upvotes: 1

Related Questions