Home > Backend Development > Python Tutorial > Efficient random number generation using numpy

Efficient random number generation using numpy

WBOY
Release: 2024-01-03 12:12:26
Original
478 people have browsed it

Efficient random number generation using numpy

Use numpy to achieve efficient random number generation

Random numbers have important applications in many fields, such as simulation experiments, initialization of machine learning algorithms, cryptography, etc. . Numpy is an efficient scientific computing library that also provides rich functions and tools for generating random numbers. This article will introduce how to use numpy to efficiently generate random numbers and give specific code examples.

The random number generation functions in the numpy library are mainly concentrated in the random module. The following are some commonly used random number generation functions and examples of their use:

  1. Generate random numbers that obey uniform distribution

Uniformly distributed random numbers within a given interval equally likely to be generated. Numpy provides the rand function to generate uniformly distributed random numbers. The code example is as follows:

import numpy as np

# 生成一个服从[0, 1)区间均匀分布的随机数
random_num = np.random.rand()
print(random_num)

# 生成一个服从[10, 20)区间均匀分布的随机数
random_num = np.random.uniform(10, 20)
print(random_num)

# 生成一个3x3的数组,其中的元素服从[0, 1)区间均匀分布
random_array = np.random.rand(3, 3)
print(random_array)
Copy after login
  1. Generate random numbers that obey normal distribution

Normally distributed Random numbers have a bell-shaped distribution centered on the mean. The randn function is provided in numpy to generate random numbers that obey the standard normal distribution. You can also use the normal function to generate normal distributed random numbers with arbitrary mean and variance. The following is a code example:

import numpy as np

# 生成一个服从标准正态分布的随机数
random_num = np.random.randn()
print(random_num)

# 生成一个服从均值为5,方差为2的正态分布随机数
random_num = np.random.normal(5, 2)
print(random_num)

# 生成一个4x4的数组,其中的元素服从标准正态分布
random_array = np.random.randn(4, 4)
print(random_array)
Copy after login
  1. Random arrangement and selection

Sometimes you need to randomly arrange an array, or randomly select a part of elements from an array. Numpy provides shuffle and choice functions to complete these operations. The following is a code example:

import numpy as np

# 随机排列一个数组
array = np.array([1, 2, 3, 4, 5])
np.random.shuffle(array)
print(array)

# 从一个数组中随机选择3个元素
array = np.array([1, 2, 3, 4, 5])
random_choice = np.random.choice(array, size=3, replace=False)
print(random_choice)
Copy after login
  1. Generate random integers

In addition to generating random floating point numbers, numpy also provides functions for generating random integers. The randint function can generate random integers within a specified range, and the choice function can also be used to generate random integers within a specified range. The following is a code example:

import numpy as np

# 生成一个[1, 10]范围内的随机整数
random_int = np.random.randint(1, 11)
print(random_int)

# 从一个数组中随机选择一个整数
array = np.array([1, 2, 3, 4, 5])
random_choice = np.random.choice(array)
print(random_choice)
Copy after login

Through the above examples, we can see that numpy provides a wealth of random number generation functions to meet the needs of various application scenarios. When generating a large number of random numbers, numpy's efficiency advantage is particularly obvious, which can greatly improve the running speed of the program.

To sum up, it is very convenient to use numpy to achieve efficient random number generation. I hope the introduction in this article can help readers better understand and use the random number generation function in the numpy library.

The above is the detailed content of Efficient random number generation using numpy. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template