Seed in Nump for the random number generator

Source: Internet
Author: User

In Python's program, the following pseudo-random number generation code was found

RNG = Numpy.random.RandomState (23355) Arraya = Rng.uniform (0,1, (2,3))

  

The purpose of this code is to produce a assarray of 2 rows and 3 columns, where each element is a uniformly distributed random number of [0,1] intervals.

Here to see, there is a 23355 this number, in fact, it is the seed of the pseudo-random number generator, that is, "the starting point for a sequence of pseudorandom"

For a pseudo-random number generator, as long as the seed (seed) is the same, the resulting sequence of random numbers is the same

Here are a few small examples

1 # still takes the seed above as an example, but executes multiple times 2 # use loops to perform 4 times 3 Import NumPy 4  for inch [1,2,3,4]: 5     RNG = Numpy.random.RandomState (23455)6     Arraya = Rng.uniform (0,1, (2,3))7      Print Arraya

The result of changing the code snippet is as follows:

As you can see, the pseudo-random numbers produced by each loop are the same, because each pseudo-random number generator has the same seed.

Now, let's look at another example.

 1  import   NumPy  2  for  i  [1,2,3,4 3  rng =     Numpy.random.RandomState (23455+i)  4  Arraya = Rng.uniform (0,1, (2,3))  5  print  ( i =%s   ' %

Here, we made a small change, each time the seed of the cycle is added I, because each cycle I value different, resulting in each cycle of the seed is also different, the following is the result of changing the program section

It can be seen that the random asarray of the 2*3 produced by each cycle are different.

Seed in Nump for the random number generator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.