'How can I generate random (int) values with specific size and mean?

I need to generate 1000 samples (int) with average 2. Do you think such a function already exists in python?



Solution 1:[1]

If you want to stick to the standard library, you can use the random libary

import random
import statistics

example = [random.randint(0, 4) for _ in range(1000)]
print(statistics.mean(example))

I arbitrarily selected 0 and 4 for the range passed to randint. You can select other ranges so long as they are centered on 2.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 somebody3697