English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NumPy Random Numbers

What are random numbers?

Random numbers do not mean that the numbers are different each time. Random means something that cannot be logically predicted.

Pseudo-random and true random

Computers work on programs, and programs are authoritative instruction sets. Therefore, this means that there must be some algorithm to generate random numbers.
If there is a program to generate random numbers, it can be predicted, so it is not a real random number.
Random numbers generated by algorithms are called pseudo-random numbers.
Can we generate real random numbers?
Yes. To generate a real random number on our computer, we need to obtain random data from some external source. External sources are usually our keystrokes, mouse movements, network data, etc.
We do not need real random numbers unless they are related to security (such as encryption keys) or the foundation of the application is randomness (such as digital roulette wheels).
In this tutorial, we will use pseudo-random numbers.

Generate random numbers

NumPy provides the random module to handle random numbers, the following demonstrates generating a number between 0 and 10random integers from 0 to

 >>> from numpy import random
 0 between random integers:100)
 >>> print(x)
 56

Generate a random floating point

The rand() method of the random module returns a number between 0 and 1 Random floating-point number between, the following demonstrates generating a number between 0 and 10Random floating-point number between 0 and

 >>> from numpy import random
 >>> x = random.rand()
 >>> print(x)
 0.4755747164243269

>>> x = random.rand()

Generate random arrays
In NumPy, we can use the two methods from the previous example to create random arrays, the randint() method accepts the size parameter, where you can specify the shape of the array. 1-The following demonstrates generating a 5 >>> x = random.randint( 10random integers from 0 to

 >>> from numpy import random
 D array, which contains10>>> x = random.randint(5))
 >>> print(x)
 [36 14 12 91 36]

Generate an array with 3 rows 2-D array, each row containing 5 >>> x = random.randint( 10random integers from 0 to

 >>> from numpy import random
 0 between random integers:10>>> x = random.randint(3, 5))
 >>> print(x)
 [[20 64 23 6 66]
  [74 11 21 61 70, size=(
  [24 47 22 22 31]]

0] 5 random floating-point numbers. The rand() method also allows you to specify the shape of the array. The following demonstrates generating an array containing 1-D array:

 >>> from numpy import random
 >>> x = random.rand(5)
 >>> print(x)
 [0.63254731 0.0275278 0.83592219 0.41890601 0.84496798]

Generate an array with 3 rows 2-D array, each row containing 5 random numbers:

 >>> from numpy import random
 >>> x = random.rand(3, 5)
 >>> print(x)
 [[0.51798216 0.70541454 0.03600922 0.2279383 0.2184512 ]
  [0.91734846 0.07877026 0.75949221 0.7876666 0.20983625]
  [0.25935065 0.37637584 0.7793815 0.65035139 0.66673048]]

choice() generates random numbers from an array

method allows you to generate random values based on an array of values. method takes an array as a parameter and randomly returns one of its values.

 >>> from numpy import random
 >>> x = random.choice([3, 5, 7, 9])
 >>> print(x)
 5

The choice() method also allows you to return an array of values. Add a size parameter to specify the shape of the array. Generate an array of random floating-point numbers by specifying the array parameters (3、5、7 and 9)values to form a two-dimensional array:

 >>> from numpy import random
 >>> x = random.choice([3, 5, 7, 9], size=(3, 5))
 >>> print(x)
 [[7 9 7 5 9]
  [3 3 3 7 9]
  [7 5 9 3 7]]