random number generation
probability distribution
algorithm
range expansion
computer science

Expand a random range from 1–5 to 1–7

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Expanding a random range from 1 to 5 to a new range from 1 to 7 might seem straightforward, but it presents interesting challenges and solutions when considering probability distributions and unbiased transformations. This article delves into the technical methodologies by which this transformation can be achieved, ensuring each number in the new range is selected with equal probability.

Understanding the Problem

When expanding a range in random number generation, our primary goal ensures that each number in the expanded target range has an equal chance of being selected. For instance, if we roll a die that yields values from 1 to 5 and want to extend this to a die that yields values from 1 to 7, each number (1 to 7) should have a 17\frac{1}{7} probability of occurring.

Techniques for Expanding the Range

1. Rejection Sampling

Rejection sampling is one effective method to achieve this expansion. By leveraging the existing 1-to-5 generator, we can craft a process that yields a uniform random number from 1 to 7. Here's how it works:

  1. Generate Two Numbers: Utilize the original random generator to produce two numbers, x=rand(1,5)x = \text{rand}(1,5) and y=rand(1,5)y = \text{rand}(1,5).
  2. Calculate the Intermediate Result: Use the formula z=5×(x1)+yz = 5 \times (x - 1) + y. The variable zz now has a range from 1 to 25.
  3. Map to New Range: Accept zz only if it falls within the first 21 numbers (i.e. 1z211 \leq z \leq 21). For z>21z > 21, reject it and repeat the process.
  4. Transform to Target Range: If zz is accepted, compute ((z1)mod7)+1( (z - 1) \bmod 7 ) + 1. This transforms zz to a new number between 1 and 7 with equal probability.

2. Probability Analysis

Each subset of 21 possible zz values represents a complete cycle in the modulo operation. The rejection sampling ensures that numbers from 22 to 25 don't introduce bias since they're disregarded and replaced with another try. Hence, the algorithm maintains a uniform distribution:

  • Accepted Values: 1 to 21, divided evenly by 7.
  • Probability Verification: Each of the seven numbers occurs three times out of the 21 accepted numbers, i.e., 321\frac{3}{21} equals 17\frac{1}{7}.

Implementation Example

Below is a simple Python example of implementing the above strategy with comments:

python
1import random
2
3def generate_random_1_to_7():
4    while True:
5        # Generate two random numbers from 1 to 5
6        x = random.randint(1, 5)
7        y = random.randint(1, 5)
8        
9        # Calculate a combined range from 1 to 25
10        z = 5 * (x - 1) + y
11        
12        # Accept if in range 1 to 21
13        if 1 <= z <= 21:
14            return ((z - 1) % 7) + 1
15
16# Test the function
17results = [generate_random_1_to_7() for _ in range(10000)]
18print({i: results.count(i) for i in range(1, 8)})

Key Points Summary

StepAction/Description
Initial RangeUse random from 1 to 5
Extend to RangeExtend logic to reach 1 to 25
Acceptable OutputAccept values in 1 to 21
Final Range MappingTransform 1 to 21 to 1 to 7 evenly
Probability OutcomeUniform distribution over new range

Conclusion

Extending a random number generator from a smaller range to a larger one while maintaining uniformity in probability distribution involves creative use of existing generators and principles like rejection sampling. The outlined method reliably expands a range from 1 to 5 to 1 to 7 without bias, keeping choice probabilities consistent. This technique is useful in various computational applications, particularly in simulation and modeling, where randomness must be controlled with precision.


Course illustration
Course illustration

All Rights Reserved.