Home > OS >  Numpy random draw gives different results
Numpy random draw gives different results

Time:06-10

I try to get 20 data points drawn from a random distribution. It's straightforward but two methods below give different results, despite the seed is the same.

Method 1

np.random.seed(1)
x1 = np.random.uniform(low=-10, high=10, size=20)
y1 = np.random.uniform(low=20, high=80, size=20)

The result is:

x1 = [-1.65955991  4.40648987 -9.9977125  -3.95334855 -7.06488218 -8.1532281
 -6.27479577 -3.08878546 -2.06465052  0.77633468 -1.61610971  3.70439001
 -5.91095501  7.56234873 -9.45224814  3.4093502  -1.65390395  1.17379657
 -7.19226123 -6.03797022]

and

y1 = [68.04467412 78.09569454 38.80545069 61.53935694 72.58334914 73.67639981
 25.10265268 22.34328699 30.18982517 72.68855021 25.90081003 45.2664575
 77.47337181 51.9899171  61.51262684 38.93093786 61.19005566 70.07754031
 21.09729664 65.0086589 ]

Method 2

N = 20
np.random.seed(1)
points = [(np.random.uniform(-10,10), np.random.uniform(20,80)) for i in range(N)]

The result is:

[(-1.6595599059485195, 63.219469606529486), (-9.997712503653101, 38.13995435791038), (-7.064882183657739, 25.54031568612787), (-6.274795772446582, 40.73364362258286), (-2.0646505153866013, 52.32900404020142), (-1.6161097119341044, 61.11317002380557), (-5.910955005369651, 72.68704618345672), (-9.452248136041476, 60.22805061070413), (-1.6539039526574602, 53.5213897067451), (-7.192261228095324, 31.886089345092728), (6.014891373510732, 78.09569454316386), (-3.7315164368151432, 61.53935694015885), (7.527783045920767, 73.67639981023083), (-8.299115772604441, 22.343286993972942), (-6.6033916087086215, 72.68855020576478), (-8.033063323338999, 45.26645750030313), (9.157790603010039, 51.98991709838103), (3.837542279009467, 38.93093786036378), (3.7300185536316732, 70.07754031384238), (-9.634234453116164, 65.00865889669805)]

Could anyone help with explaining the difference?

CodePudding user response:

The first method first generates 20 numbers from the first distribution, followed by 20 numbers from the second distribution. In the second method, you alternate the distribution from which numbers are being generated. These methods do not generate corresponding numbers in the same order, so you should not expect to get the same results. Each time you generate a random number, the internal state of the generator changes, and that change affects all subsequent invocations of the generator, regardless of whether it is applied to the same distribution. numpy.random methods all use the same global internal state.

As an aside, NumPy recommends the use of numpy.random.Generator methods instead of numpy.random or numpy.random.RandomState methods (although they are not currently deprecated; see here).

  • Related