---
title: Random Numbers in Python
date: 2025-02-23T11:02:02Z
modified: 2026-06-22T16:12:48Z
permalink: "https://www.micheledpierri.com/2025/02/23/random-numbers-in-python/"
type: post
status: publish
excerpt: ""
wpid: 999
categories:
  - Data Analysis
  - Programming
tags:
  - Data Analysis
  - Programming
  - Python
featured_image: "https://www.micheledpierri.com/wp-content/uploads/2025/02/random_numbers_.png"
featured_image_alt: A robed, multi-armed humanoid figure sits behind a stone table in a grand vaulted hall, surrounded by glowing circular symbols and astrological diagrams, creating a mystical, sepia-toned scene with an antique fresco-like atmosphere.
timestamp: 2026-06-22T16:12:48Z
---

## Why do we need random number generation in statistics and data science?

Data scientists and statisticians rely on random number generation for several important purposes.

They can be used to create data samples, which serves as a foundation for advanced statistical techniques. This includes Bootstrapping methods that involve resampling from existing data to create new samples and [Monte Carlo Simulation](https://www.micheledpierri.com/elements-of-statistics-for-doctors/monte-carlo-simulation/) approaches that generate synthetic data points based on probability distributions. These techniques are particularly valuable when researchers need to expand their sample sizes, validate statistical models, estimate uncertainty in their analyses, and conduct complex simulations to understand system behavior under various conditions. For example, these functions are particularly useful when real data is scarce for testing algorithms—in medicine, researchers can generate simulated patient data to test predictive models

When designing neural networks, the initial weights are generally set randomly to avoid symmetries and achieve good learning outcomes. This randomization process is crucial because it helps prevent all neurons from learning the same features during training. Additionally, random initialization helps break the symmetry between neurons in the same layer, allowing each neuron to specialize in detecting different patterns in the input data

In decision trees, random numbers play a crucial role in feature selection and splitting criteria. During the tree construction process, a random selection of features at each split point helps create more diverse and robust models by introducing an element of randomization. .

In Machine Learning model training processes, random numbers play a vital role in [dataset partitioning](https://www.micheledpierri.com/wp-content/uploads/wp-mfa-exports/page/dataset-division-and-data-leakage.md). Practitioners typically divide their original dataset into separate training and testing sets using random sampling techniques when preparing data for model training and evaluation. This randomization ensures an unbiased distribution of data points across these sets, which is crucial for accurately assessing model performance. .

## Generating Random Numbers in Python

Python provides several ways to generate random numbers through different libraries: random (part of the standard library), NumPy, PyTorch, secrets, and os.

### Random numbers with [random](https://docs.python.org/3/library/random.html)

import random
print(random.random())  # Random number between 0 and 1
print(random.randint(1, 100))  # Integer between 1 and 100 (inclusive)
print(random.randrange(0, 100, 5))  # Integer between 0 and 100 (multiple of 5)
```
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> random</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(random.random())  </span><span style="color: #6272A4"># Random number between 0 and 1</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(random.randint(</span><span style="color: #BD93F9">1</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">100</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Integer between 1 and 100 (inclusive)</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(random.randrange(</span><span style="color: #BD93F9">0</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">100</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">5</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Integer between 0 and 100 (multiple of 5)</span></span>
<span class="line"></span>
```

The random module also allows you to randomly select elements from a list or shuffle a list’s contents:

items = ["apple", "banana", "cherry"]
print(random.choice(items))  # Select a random element
print(random.choices(items, k=2))  # Select 2 elements with replacement
print(random.sample(items, 2))  # Select 2 elements without replacement

numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)  # Shuffle the list elements
print(numbers)
```
<span class="line"><span style="color: #F8F8F2">items </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> [</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">apple</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">, </span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">banana</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">, </span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">cherry</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">]</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(random.choice(items))  </span><span style="color: #6272A4"># Select a random element</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(random.choices(items, </span><span style="color: #FFB86C; font-style: italic">k</span><span style="color: #FF79C6">=</span><span style="color: #BD93F9">2</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Select 2 elements with replacement</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(random.sample(items, </span><span style="color: #BD93F9">2</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Select 2 elements without replacement</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F8F8F2">numbers </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> [</span><span style="color: #BD93F9">1</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">2</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">4</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">5</span><span style="color: #F8F8F2">]</span></span>
<span class="line"><span style="color: #F8F8F2">random.shuffle(numbers)  </span><span style="color: #6272A4"># Shuffle the list elements</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(numbers)</span></span>
<span class="line"></span>
```

### Random number with [numpy.random](https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html)

NumPy’s random function provides multiple random number generation capabilities: generating values between 0 and 1 (random.rand), integers (random.randint), and manipulating lists through random selection (random.choice) or shuffling (random.shuffle). It also enables the generation of data according to common statistical distributions, including normal (random.normal) and uniform (random.uniform) distributions.

import numpy as np

print(np.random.rand())  # Float between 0 and 1
print(np.random.rand(3))  # Array with 3 floats
print(np.random.rand(2, 3))  # 2x3 matrix of floats

print(np.random.randint(1, 100))  # An integer between 1 and 100
print(np.random.randint(1, 100, 5))  # Array with 5 integers

arr = np.array([10, 20, 30, 40])
print(np.random.choice(arr))  # Random element
np.random.shuffle(arr)  # Shuffle the array
print(arr)

print(np.random.normal(0, 1, 5))  # 5 numbers from normal distribution (mean=0, std.dev=1)
print(np.random.uniform(0, 10, 5))  # 5 numbers from uniform distribution [0,10]
```
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> numpy </span><span style="color: #FF79C6">as</span><span style="color: #F8F8F2"> np</span></span>
<span class="line"></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.rand())  </span><span style="color: #6272A4"># Float between 0 and 1</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.rand(</span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Array with 3 floats</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.rand(</span><span style="color: #BD93F9">2</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># 2x3 matrix of floats</span></span>
<span class="line"></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.randint(</span><span style="color: #BD93F9">1</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">100</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># An integer between 1 and 100</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.randint(</span><span style="color: #BD93F9">1</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">100</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">5</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Array with 5 integers</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F8F8F2">arr </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> np.array([</span><span style="color: #BD93F9">10</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">20</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">30</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">40</span><span style="color: #F8F8F2">])</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.choice(arr))  </span><span style="color: #6272A4"># Random element</span></span>
<span class="line"><span style="color: #F8F8F2">np.random.shuffle(arr)  </span><span style="color: #6272A4"># Shuffle the array</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(arr)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.normal(</span><span style="color: #BD93F9">0</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">1</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">5</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># 5 numbers from normal distribution (mean=0, std.dev=1)</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.uniform(</span><span style="color: #BD93F9">0</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">10</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">5</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># 5 numbers from uniform distribution [0,10]</span></span>
<span class="line"></span>
```

### Random numbers with [torch](https://pytorch.org/docs/stable/random.html)

The PyTorch library supports both CPU and GPU processing

import torch

print(torch.rand(1))  # Float between 0 and 1
print(torch.rand(3, 3))  # 3x3 Matrix
print(torch.randint(0, 100, (5,)))  # Tensor with 5 integers
print(torch.randn(5))  # Standard normal distribution
print(torch.normal(mean=0, std=1, size=(3,)))  # Normal distribution with mean=0, std=1

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(torch.rand(3, device=device))  # Random tensor on GPU
```
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> torch</span></span>
<span class="line"></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(torch.rand(</span><span style="color: #BD93F9">1</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Float between 0 and 1</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(torch.rand(</span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># 3x3 Matrix</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(torch.randint(</span><span style="color: #BD93F9">0</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">100</span><span style="color: #F8F8F2">, (</span><span style="color: #BD93F9">5</span><span style="color: #F8F8F2">,)))  </span><span style="color: #6272A4"># Tensor with 5 integers</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(torch.randn(</span><span style="color: #BD93F9">5</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Standard normal distribution</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(torch.normal(</span><span style="color: #FFB86C; font-style: italic">mean</span><span style="color: #FF79C6">=</span><span style="color: #BD93F9">0</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">std</span><span style="color: #FF79C6">=</span><span style="color: #BD93F9">1</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">size</span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2">(</span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">,)))  </span><span style="color: #6272A4"># Normal distribution with mean=0, std=1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F8F8F2">device </span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2"> torch.device(</span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">cuda</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2"> </span><span style="color: #FF79C6">if</span><span style="color: #F8F8F2"> torch.cuda.is_available() </span><span style="color: #FF79C6">else</span><span style="color: #F8F8F2"> </span><span style="color: #E9F284">"</span><span style="color: #F1FA8C">cpu</span><span style="color: #E9F284">"</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(torch.rand(</span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">, </span><span style="color: #FFB86C; font-style: italic">device</span><span style="color: #FF79C6">=</span><span style="color: #F8F8F2">device))  </span><span style="color: #6272A4"># Random tensor on GPU</span></span>
<span class="line"></span>
```

### Random numbers with [secrets](https://docs.python.org/3/library/secrets.html)

The secrets library generates cryptographically secure random numbers, unlike the pseudo-random numbers provided by the previous libraries. This makes it the ideal choice for generating passwords, security tokens, and cryptographic keys.

import secrets

print(secrets.randbelow(100))  # Number between 0 and 99
print(secrets.token_bytes(16))  # 16 random bytes
print(secrets.token_hex(16))  # 16 bytes in hexadecimal format
print(secrets.token_urlsafe(16))  # Secure URL token
```
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> secrets</span></span>
<span class="line"></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(secrets.randbelow(</span><span style="color: #BD93F9">100</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Number between 0 and 99</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(secrets.token_bytes(</span><span style="color: #BD93F9">16</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># 16 random bytes</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(secrets.token_hex(</span><span style="color: #BD93F9">16</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># 16 bytes in hexadecimal format</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(secrets.token_urlsafe(</span><span style="color: #BD93F9">16</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Secure URL token</span></span>
<span class="line"></span>
```

### Random numbers with [os](https://docs.python.org/3/library/os.html)

The os library also provides truly random numbers by generating them from the system’s kernel.

import os

print(os.urandom(8))  # 8 byte casuali

```
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> os</span></span>
<span class="line"></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(os.urandom(</span><span style="color: #BD93F9">8</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># 8 byte casuali</span></span>
<span class="line"></span>
<span class="line"></span>
```

### Summary



| Library | Main Function | Uses Seed? | Main Purpose |
| --- | --- | --- | --- |
| random | random.random() | Yes | Simulations, games |
| numpy | np.random.rand() | Yes | Machine Learning, statistics |
| torch | torch.rand() | Yes | Deep learning(CPU/GPU) |
| secrets | secrets.randbelow() | No | Cryptography, passwords |
| os | os.urandom() | No | Secure system random numbers |

## Differences Between Pseudo-Random and True Random Numbers

Random number generators can be classified into two distinct categories: true random number generators (**TRNG** = True Random Number Generator) and pseudo-random number generators (**PRNG**). True random number generators derive their randomness from physical processes or phenomena that are inherently unpredictable, such as atmospheric noise, radioactive decay, or thermal fluctuations. In contrast, pseudo-random number generators use mathematical algorithms to generate sequences of numbers that appear random but are deterministic when given the same initial conditions or seed.

Within the Python ecosystem, this distinction is reflected in the implementation of various libraries: the r_andom, numpy_, and _torch_ libraries implement pseudo-random number generators for their efficiency and reproducibility in scientific computing and machine learning applications, while the _secrets_ and _os_ libraries utilize system-level sources of entropy to provide true random numbers suitable for cryptographic purposes.

### Pseudo-random number generators

For pseudo-random number generation, NumPy employs either the Mersenne Twister (MT19937) algorithm or the newer Permuted Congruential Generator (PCG64), while PyTorch primarily uses the Philox algorithm alongside MT19937.

A random number generator’s period is the maximum number of values it outputs before the sequence starts repeating. For instance, in the sequence 3,2,8,5,6,3,2,8,5,6,3,2,8, the period is 5 since the pattern repeats after every five numbers.

The periods of these random number generators are compared in the table below. While MT19937 has an extraordinarily long period, PCG64 and Philox offer faster performance despite their shorter periods.



| Algorithm | Period |
| --- | --- |
| MT19937 | 2^19937 -1 |
| PCG64 | 2^128 |
| Philox | 2^256 |

### True random number generators

True random number generators don’t rely on algorithms—instead, they harness system entropy. In computing, entropy refers to the degree of unpredictability and disorder within a system.

Sources of entropy include:

- Mouse movements: timing, position, and motion patterns provide unpredictable yet measurable data
- Keyboard input: the timing and patterns of keystrokes serve as unpredictable events
- Voltage fluctuations in electronic circuits
- Network activity: the timing of incoming data packets on internet and network connections
- Storage performance: variations in disk read latency and speed

The computer collects entropy data from various sources and continuously updates it in the system kernel. Specifically, Linux uses /dev/random and /dev/urandom, Windows uses CryptGenRandom(), and iOS uses SecRandomCopyBytes().

The secrets and os libraries draw from these system sources to generate truly random numbers.

## Setting Seeds to Control Random Number Generation

Libraries that use pseudo-random number generation algorithms, specifically NumPy and PyTorch, let you “seed” the random number generator to produce consistent results across different runs.

There are several reasons why developers and data scientists may need to “fix” or control random number generation in their applications. During the debugging process, having consistent and predictable values makes it much easier to track down and identify potential errors in the code. When conducting scientific experiments or research that involves generating data samples, fixed random number generation ensures that the experiments are reproducible by other researcher. Additionally, when evaluating and comparing the performance of different algorithms or machine learning models, having consistent random numbers across all tests improves the validity of the comparisons by eliminating random variation as a confounding factor. These controlled conditions allow for more accurate and meaningful assessments of algorithmic performance.

This reproducibility is achieved by using the seed() function.

In NumPy, you can set the seed using the random.seed(x) function, where x is any number of your choice.

import numpy as np

np.random.seed(42)
print(np.random.rand(3))  # Generates a fixed sequence of numbers

np.random.seed(42)
print(np.random.rand(3))  # Reproduces the exact same sequence
```
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> numpy </span><span style="color: #FF79C6">as</span><span style="color: #F8F8F2"> np</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F8F8F2">np.random.seed(</span><span style="color: #BD93F9">42</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.rand(</span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Generates a fixed sequence of numbers</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F8F8F2">np.random.seed(</span><span style="color: #BD93F9">42</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.rand(</span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Reproduces the exact same sequence</span></span>
<span class="line"></span>
```

In PyTorch, the seeding function is manual\_seed(x)

import torch

torch.manual_seed(42)
print(torch.rand(3))  # Always generates the same numbers

torch.manual_seed(42)
print(torch.rand(3))  # Reproduces the same sequence
```
<span class="line"><span style="color: #FF79C6">import</span><span style="color: #F8F8F2"> torch</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F8F8F2">torch.manual_seed(</span><span style="color: #BD93F9">42</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(torch.rand(</span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Always generates the same numbers</span></span>
<span class="line"></span>
<span class="line"><span style="color: #F8F8F2">torch.manual_seed(</span><span style="color: #BD93F9">42</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(torch.rand(</span><span style="color: #BD93F9">3</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Reproduces the same sequence</span></span>
<span class="line"></span>
```

When a seed sequence is set, all subsequent random numbers generated by the program will follow that same sequence.

You can reset this sequence by changing the seed value to a different number:

np.random.seed(42)
print(np.random.randint(0, 100))  # Generate first number in sequence
np.random.seed(99)  # Set new seed
print(np.random.randint(0, 100))  # Generate number from new sequence
```
<span class="line"><span style="color: #F8F8F2">np.random.seed(</span><span style="color: #BD93F9">42</span><span style="color: #F8F8F2">)</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.randint(</span><span style="color: #BD93F9">0</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">100</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Generate first number in sequence</span></span>
<span class="line"><span style="color: #F8F8F2">np.random.seed(</span><span style="color: #BD93F9">99</span><span style="color: #F8F8F2">)  </span><span style="color: #6272A4"># Set new seed</span></span>
<span class="line"><span style="color: #8BE9FD">print</span><span style="color: #F8F8F2">(np.random.randint(</span><span style="color: #BD93F9">0</span><span style="color: #F8F8F2">, </span><span style="color: #BD93F9">100</span><span style="color: #F8F8F2">))  </span><span style="color: #6272A4"># Generate number from new sequence</span></span>
<span class="line"></span>
```

Using 42 as a seed value is a common convention in the developer community. While any number can serve as a seed value, 42 has become particularly widespread.

This popularity originates from practical reasons: it’s easy to remember, and its widespread use makes it simpler to compare results between different developers.

Additionally, the number has cultural significance—it’s famously cited in Douglas Adams’ “The Hitchhiker’s Guide to the Galaxy” as “the ultimate answer to life, the universe and everything.” Using 42 has thus become a playful reference that developers often share.

## Further Reading

[ https://www.wan.io/random-number-generator-works/](https://www.wan.io/random-number-generator-works/)

[Wikipedia — Applications of randomness – Wikipedia](https://en.wikipedia.org/wiki/Applications_of_randomness)

[These Numbers Look Random but Aren’t, Mathematicians Prove | Scientific American](https://www.scientificamerican.com/article/these-numbers-look-random-but-arent-mathematicians-prove/)

## Summary and Conclusions

In the fields of statistics, machine learning, and scientific research, random numbers play a crucial role in various applications. Python offers a comprehensive ecosystem for random number generation through two main approaches: Pseudo-random number generators (PRNG) and True random number generators (TRNG).

The choice between PRNG and TRNG depends on your specific use case – use PRNGs when reproducibility is important, and TRNGs when true randomness is required for security purposes.