2

I need to generate an array in numpy (there are N numbers).

There are only two kinds of element in this array, for example: 3.0 and -3.0. The probability of occurring 3.0 is 0.4, and the probability of occurring -3.0 is 0.6.

How to generate such an array?

dodolong
  • 855
  • 2
  • 9
  • 14

1 Answers1

4

This will do the job:

n=10
np.random.choice([3,-3],n,p=[0.4,0.6])
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76