Explanation of random number seeds: principle, application and example
In programming, random numbers are a very important function and are widely used in scientific computing, data processing, machine learning, and game development. However, random numbers are not really "random", but "pseudo Random Number" generated by a specific algorithm. In order for the program to generate consistent random numbers in specific situations, we can use the Random Seed to control the generation process of random numbers. This article will explain in-depth the concept, principles and applications of random number seeds, and demonstrate through examples how to set random number seeds.
1. What are random number seeds?
Random number seeds (Seed) are the initial values of pseudo-random number generators. Pseudo-random numbers are generated by specific algorithms, they seem random, but are actually deterministic. If the initial state (seed) of the pseudo-random number generator is the same, the sequence of random numbers generated each time will be exactly the same.
Simply put, a random number seed is a "switch" that controls the generation of random numbers. After setting the seed, the behavior involving random numbers in the program becomes "controllable and reproducible".
Why are pseudo-random numbers deterministic?
- Pseudo-random numbers are generated by mathematical formulas or algorithms.
- Given the same input conditions (such as seed values), the algorithm generates the same sequence of output random numbers.
- Therefore, by controlling seeds, we can control random behavior and ensure the reproducibility of the experiment.
2. The role of random number seeds
1. Ensure the reproducibility of the results
In many experiments, especially in machine learning or scientific research, the reproducibility of results is crucial. If the program generates different random numbers each time it runs, debugging and comparing experiments can become very difficult. By setting random number seeds, you can ensure that the results of random operations are consistent every time the program is run.
2. Debugging is more convenient
In the code, if some operations rely on randomness (such as random initialization of neural network weights, random segmentation of data sets, etc.), not setting a random number seed may cause inconsistent results for each run. By setting the seed, the program can maintain the same sequence of random numbers every time it runs, making it easier to discover and solve problems.
3. Control random behavior
In game development or simulation experiments, sometimes it is necessary to generate "seemingly random" behaviors, but at the same time, they are expected to reproduce them under certain conditions. Setting a random number seed can meet this requirement.
3. How to set random number seeds?
Python provides a variety of random number generators, the common ones are:
-
random
Module: A random number generator provided by the standard library, suitable for simple scenarios. -
numpy
Random module: used for scientific computing. - Random modules of deep learning frameworks (such as PyTorch and TensorFlow): used to control randomness during training.
The following explains their usage separately.
1. Python random module
()
Used to set the seeds of the random number generator to ensure that the same sequence of random numbers is generated every run.
Example:
import random (42) # Set random number seedsprint((1, 100)) # The output is fixed, for example: 81print(()) # The output is fixed, for example: 0.6394267984578837 # Set the same seed again, the result is the same(42) print((1, 100)) # The output is still: 81print(()) # The output is still: 0.6394267984578837
Summary: As long as the seeds are fixed,random
The random number sequence generated by the module is exactly the same.
2. NumPy's random module
NumPy provides more powerful random number function, which can also be passed()
Set seeds.
Example:
import numpy as np (42) # Set random number seedsprint((3)) # The output is fixed, for example: [0.37454012 0.95071431 0.73199394] # Set the same seed again, the result is the same(42) print((3)) # The output is still: [0.37454012 0.95071431 0.73199394]
3. Random number seeds of PyTorch
In deep learning, randomness is often used for neural network weight initialization, data enhancement, etc. PyTorch providestorch.manual_seed()
To control the generation of random numbers.
Example:
import torch torch.manual_seed(42) # Set random number seedsprint((3)) # The output is fixed, for example: tensor([ 0.3367, 0.1288, 0.2341]) # Set the same seed again, the result is the sametorch.manual_seed(42) print((3)) # The output is still: tensor([0.3367, 0.1288, 0.2341])
Note: If using GPU, you also need to set:
.manual_seed(42) .manual_seed_all(42)
4. Complete examples of combining code
Here is a complete example showing how to control the reproducibility of random behavior through random number seeds:
import random import numpy as np import torch # Set global seedsmanual_seed = (1, 10000) # Generate a seed randomly(manual_seed) # Set Python random number seeds(manual_seed) # Set NumPy random number seedstorch.manual_seed(manual_seed) # Set PyTorch random number seeds # Example 1: Random integersprint((1, 100)) # The result is fixedprint((3)) # The result is fixedprint((3)) # The result is fixed # Example 2: Generate consistent results with the same seeds(manual_seed) (manual_seed) torch.manual_seed(manual_seed) print((1, 100)) # The result is still fixedprint((3)) # The result is still fixedprint((3)) # The result is still fixed
5. Things to note
Range of seed values:
- The seed value is usually a non-negative integer. Excessive seeds may exceed the generator's calculation range (such as 32-bit system limit).
Scope of impact:
-
()
Only affecting Pythonrandom
Modules, will not affect the random numbers of NumPy or PyTorch. - To control the randomness of multiple modules at the same time, seeds need to be set separately.
Randomity of GPU:
- When using GPU, some operations may still have uncontrollable randomness (such as non-deterministic CUDA algorithms), and additional settings are required
= True
。
6. Summary
The role of random number seeds can be summarized as follows:
- Control random behavior: After fixed seeds, the same random number sequence will be generated every time the program is run.
- Improve code reproducibility: especially in scientific research and machine learning tasks.
- Easy to debug: It can make the experimental results consistent and facilitate positioning of problems.
Whether it is Pythonrandom
Modules, NumPy's random modules, or deep learning frameworks (such as PyTorch), all provide seed setting functions. By rationally using random number seeds, the program's behavior can be ensured to be more stable and reliable.
This is the end of this article about setting up a random seed. For more related content on Python random seeds, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!