Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

NumPy’s random.randint(low, high=None, size=None, dtype=int) generates "random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high)".

Therefore, to generate random integers with mean of 2 just make sure that the interval you specify is centred on 2.

>>> import numpy as np
>>> np.random.randint(0,5,10000000).mean()
1.999827
>>> np.random.randint(-10,15,10000000).mean()
2.000426
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading