Random module in Numpy

To work with Random numbers, Numpy has a module called random. To use the module, import it at the beginning of a Python program as shown below:

Let us see some examples:

  • Generate a random number
  • Generate a random array with a fixed size
  • Generate one of the random values based on an array of values

Generate a Random number

To generate a random number, we have used the randint() method of the random module. Let us see an example:

Output

Generate a random array with a fixed size

We will generate random elements of an array using the random.randint() method. The size of the array is set using the size parameter. Let us see an example:

Output

Generate one of the random values based on an array of values

One of the random values based on an array of values can be generated using the choice() method of the random module. Let us see an example:

Output

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

Statistical Operations on Numpy arrays
Numpy Logs
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading