liveBook · Manning

Random number

As explained in Statistics Every Programmer Needs

A random number is a generated numerical value treated as a sample from a specified probability distribution or numerical interval.

A random number is a generated value used to represent one possible outcome from a specified probability distribution or numerical interval. In Monte Carlo simulation, random numbers convert modeled uncertainty into simulated scenarios: each value is mapped to an outcome according to cumulative probabilities. section 9.2

A practical simulation workflow uses random numbers to:

  • Translate a discrete probability distribution into cumulative-probability intervals. section 9.2
  • Assign two-digit values from 00 through 99 to possible outcomes in proportion to their probabilities. section 9.3.3
  • Generate reproducible samples with a fixed seed, using sampling without replacement for smaller runs or with replacement for larger runs. section 9.3.4
  • Run repeated trials by mapping each generated value to an outcome and analyzing the resulting distribution. section 9.3.5

Absenteeism staffing simulation

A call-center model assigns random digits to values of k under a Poisson distribution. Digit 73 maps to k = 3, indicating three additional associates for that trial rather than the rate-based expectation of two. Across 10 trials, the simulated outcomes have a mean of 2.4 additional staff, helping balance understaffing risk against overstaffing cost. section 9.3.5 section 9.3.3

>>> random_digits = [f'{i:02}' for i in range(0, 100)]     #1
>>> random_digits_ranges = []     #2
>>> current_index = 0     #3
>>> total_digits = len(random_digits)    #4
>>> for i, row in poisson_table.iterrows():    #5
>>>     count = max(1, int(row['Probability (%)']))    #6
>>>     end_index = (current_index + count - 1) % total_digits    #7
>>>     if current_index <= end_index:     #8
>>>         start = random_digits[current_index]     #9
>>>         end = random_digits[end_index]     #10
>>>         random_digits_ranges.append(f’{start}-‘ \
>>>                                     f’{end}’)     #11
>>>     else:     #12
>>>         start = random_digits[current_index]     #13
>>>         end = random_digits[end_index]     #14
>>>         random_digits_ranges.append(f’{start}-99, ‘ \
>>>                                     f’00-{end}')   #15
>>>     current_index = \
>>>         (end_index + 1) % total_digits     #16
>>> poisson_table['Random Digits'] = \
>>>.    random_digits_ranges     #17
>>> print(poisson_table)     #18






   k  Probability (%)  Cumulative Probability (%) Random Digits
0  0            13.53                       13.53         00-12
1  1            27.07                       40.60         13-39
2  2            27.07                       67.67         40-66
3  3            18.04                       85.71         67-84
4  4             9.02                       94.73         85-93
5  5             3.61                       98.34         94-96
6  6             1.20                       99.55         97-97
7  7             0.34                       99.89         98-98
8  8             0.09                       99.98         99-99

Inspect Python code that allocates two-digit ranges to discrete outcomes according to probability percentages, including ranges that wrap from 99 to 00. section 9.3.3

>>> np.random.seed(1)   #1
>>> random_digits = \
>>>     np.random.choice(100, 10, replace = False)     #2
>>> formatted_digits = \
>>>     [f'{digit:02}' for digit in random_digits]   #3

Inspect NumPy code that seeds the generator, samples ten values, and formats them as two-digit strings. section 9.3.4

Table 9.1 Random numbers aligned to k values
Random number
k value
80
84
33
81
93
17
36
3
3
1
3
4
1
1
82
69
65
Sum
Mean
3
3
2
24
2.4

Inspect the trial table aligning generated random numbers with k values and calculating the total and mean staffing requirement. section 9.3.5

How Statistics Every Programmer Needs covers it

How deep it goes
Traces interval construction, seeded generation, replacement choices, and repeated-trial interpretation, but does not develop generator internals.
Examples
Uses 00–99 digit ranges, seeded NumPy samples, and call-center absenteeism trials producing a mean staffing requirement of 2.4.
What it assumes
Assumes specified probabilities and outcome values, then supplies the interval-mapping procedure needed to simulate discrete outcomes.
What you can do after
Apply it via Python and NumPy to generate reproducible samples, map them to outcomes, and estimate variability across trials.
How it connects
Connects random numbers to Monte Carlo simulation, probability distributions, cumulative distribution functions, Poisson distributions, and random seeds.

Also explained in

Compare how 4 books explain random number →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.