Python : How to Generate positive number with a given standard deviation and mean within 0-1?

Viewed 55

I want to generate a list (or array) of all positive numbers, and all the numbers need to be within 0-1.

I've seen one code, numpy.random.normal([mean], [standard deviation], [array size]). But it generate both positive an negative numbers.

Any other codes/formula that can solve it?


Based on the below codes provided by Cardstdani, I found that
np.random.normal([mean], [STD], [sample size]), the mean value would be probably incorrect after checking it. For example, if mean = 1.13, STD = 0.339 and size = 4.

1 Answers

Use absolute value function np.abs() and mod() to apply modulo operator with the max value that you want:

import numpy as np
print(np.mod(np.abs(np.random.normal([0.5], [1], [2])), 1))

Output:

[0.58674917 0.28187618]
Related