Home > Backend Development > Python Tutorial > How to Generate Random Numbers Summing to a Predefined Value with Equal Probability?

How to Generate Random Numbers Summing to a Predefined Value with Equal Probability?

Susan Sarandon
Release: 2024-10-27 05:14:02
Original
317 people have browsed it

How to Generate Random Numbers Summing to a Predefined Value with Equal Probability?

Generating Random Numbers Summing to a Predefined Value

In this context, we aim to generate a list of pseudo-random numbers that collectively add up to a specific predetermined value. One method involves randomly generating a number within a specified range, subtracting it from the total, and repeating this process until the sum equals the desired value. However, this approach favors the first generated number in terms of its contribution to the sum.

To ensure uniformity, a more sophisticated solution has been developed:

<code class="python">import random

def constrained_sum_sample_pos(n, total):
    """Return a randomly chosen list of n positive integers summing to total.
    Each such list is equally likely to occur."""

    dividers = sorted(random.sample(range(1, total), n - 1))
    return [a - b for a, b in zip(dividers + [total], [0] + dividers)]</code>
Copy after login

This method generates a list of positive integers that sum to the target value. The key concept is that every possible combination of numbers is equally likely to be generated. Additionally, extending this technique to different totals and varying numbers of random numbers is straightforward.

For instance, if we set n to 4 and total to 40, we might obtain the following output: [4, 4, 25, 7]. It's important to note that each element of this list, when summed, produces the predefined value of 40.

To allow for non-positive integers, a modification is available:

<code class="python">def constrained_sum_sample_nonneg(n, total):
    """Return a randomly chosen list of n nonnegative integers summing to total.
    Each such list is equally likely to occur."""

    return [x - 1 for x in constrained_sum_sample_pos(n, total + n)]</code>
Copy after login

By incrementing each value by one, we can generate a list of non-negative integers that add up to the desired total.

This method ensures that every possible combination, regardless of whether the numbers are positive or non-negative, has an equal probability of being generated. With its user-friendly implementation and versatility across different scenarios, constrained_sum_sample_pos and constrained_sum_sample_nonneg have become integral to random number generation tasks in Python.

The above is the detailed content of How to Generate Random Numbers Summing to a Predefined Value with Equal Probability?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template