Search code examples
pythonstatisticsdatasetprobability

Generating datasets with a certain probability


Suppose I have 60% of red balls and 40% of blue balls. How can I generate 1000 datasets each containing 10 balls? (Python)


Solution

  • You can use random.choices to pick items from a collection randomly, based on weights provided.

    dataset = random.choices(
        population=['blue', 'red'],
        weights=[0.4, 0.6],
        k=10
    )
    

    Sample output: ['red', 'blue', 'blue', 'blue', 'blue', 'blue', 'red', 'red', 'blue', 'red']