[Fifty-third day of getting started with Python] Random numbers in Python丨NumPy

What are random numbers?

Random numbers don't mean different numbers every time. Random means something that cannot be predicted logically.

pseudo random and true random

Computers work on programs, which are authoritative sets of instructions. So this means there must be some kind of algorithm to generate random numbers.

If there is a program that generates random numbers, it can be predicted, so it is not truly random.

Random numbers generated by a generative algorithm are called pseudo-random numbers.

Can we generate truly random numbers?

Yes. In order to generate a truly random number on our computer, we need to get random data from some external source. External sources are usually our keystrokes, mouse movements, network data, etc.

We don't need truly random numbers unless it's related to security (eg encryption keys) or the basis of the application is randomness (eg digital roulette wheel).

In this tutorial, we will use pseudorandom numbers.

Generate random numbers

NumPy provides the random module to handle random numbers.

example

Generate a random integer between 0 and 100:

from numpy import random

x = random.randint(100)

print(x)

run instance

generate random floating point

The rand() method of the random module returns a random floating point number between 0 and 1.

example

Generate a random floating point number between 0 and 100:

from numpy import random

x = random.rand()

print(x)

run instance

generate random array

In NumPy, we can use the two methods in the above example to create random arrays.

The integer
randint() method accepts a size parameter where you can specify the shape of the array.

example

Generate a 1-D array of 5 random integers from 0 to 100:

from numpy import random

x=random.randint(100, size=(5))

print(x)

run instance

example

Generate a 2-D array with 3 rows, each containing 5 random integers from 0 to 100:

from numpy import random

x = random.randint(100, size=(3, 5))

print(x)

run instance

floating point number

The rand() method also allows you to specify the shape of the array.

example

Generate a 1-D array of 5 random floating-point numbers:

from numpy import random

x = random.rand(5)

print(x)

run instance

example

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

from numpy import random

x = random.rand(3, 5)

print(x)

run instance

Generate random numbers from an array

The choice() method allows you to generate random values ​​based on an array of values.

The choice() method takes an array as a parameter and returns one of the values ​​at random.

example

Return one of the values ​​in the array:

from numpy import random

x = random.choice([3, 5, 7, 9])

print(x)

run instance

The choice() method also allows you to return an array of values.

Please add a size parameter to specify the shape of the array.

example

Generate a two-dimensional array of the values ​​in the array arguments (3, 5, 7, and 9):

from numpy import random

x = random.choice([3, 5, 7, 9], size=(3, 5))

print(x)

run instance

Guess you like

Origin blog.csdn.net/ooowwq/article/details/129826431