Skip to main content

generate_random_distribution

Function generate_random_distribution 

Source
pub fn generate_random_distribution(
    n: usize,
    total: u128,
    min: u128,
    max: u128,
) -> Result<Vec<u128>>
Expand description

Generate a random distribution of amounts across n recipients.

Each amount will be in the range [min, max] and all amounts will sum to exactly total.

§Algorithm

  1. Start everyone at the minimum amount
  2. Randomly distribute the remaining amount (total - n*min) across recipients
  3. Shuffle the final amounts to avoid bias toward earlier recipients

§Errors

Returns an error if the constraints are unsatisfiable:

  • n * min > total (not enough total to give everyone the minimum)
  • n * max < total (can’t fit the total even with everyone at maximum)