Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # See https://softwareengineering.stackexchange.com/questions/400487
- # and https://softwareengineering.stackexchange.com/a/400497/350119
- # for what this
- def gen_weighted_random_fixed_sum_bool_list(size, amount_ones, weights):
- import random
- # Copy the list to prevent modification of a passed-in object
- weights = list(weights)
- # List of all possible indices for random.choices to choose from
- all_indices = list(range(size))
- result = [0] * size
- for i in range(amount_ones):
- # Choose an index to set True
- index = random.choices(all_indices, weights)[0]
- result[index] = 1 # Set index True
- weights[index] = 0 # Remove that index from the list
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement