How do I generate some random numbers with a distribution using Python?

0 votes

Here is one file with probabilities for different values. Let's say for example:

1 0.2
2 0.25
3 0.35
4 0.4
5 0.5
6 0.3

I would like to generate completely random numbers using the distribution. Is there an existing module that handles this? 

Oct 31, 2018 in Python by Nymeria
• 3,500 points

edited Dec 14, 2018 by Nymeria 109 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

scipy.stats.rv_discrete is what you ned IMHO.

You can supply your probabilities via the values parameter and it's really easy and you will figure it out. Also, you can then use the rvs() method to generate random numbers when needed too.

You can also pass a keyword parameter to numpy.random.choice(),

Consider the following example:

numpy.random.choice(numpy.arange(1, 7), p=[0.35, 0.27, 0.35, 0.43, 0.1, 0.12]
answered Oct 31, 2018 by Anirudh
• 2,050 points

edited Dec 14, 2018 by Anirudh

Related Questions In Python

0 votes
1 answer

How do I create 3 X 4 array of random numbers between 0 and 100 using python?

Use numpy in the following manner: np.random.rand(3,4)*100 When you ...READ MORE

answered May 24 in Python by Radhika
11 views
0 votes
1 answer

How do I get a random number with a float range in python?

Use random.uniform(a, b): Returns a random floating point ...READ MORE

answered May 27 in Python by Kiran
11 views
0 votes
1 answer

How can I lookup hostname using the IP address with a timeout in Python?

Good question. I actually was stuck with ...READ MORE

answered Feb 6 in Python by Nymeria
• 3,500 points
46 views
0 votes
1 answer
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
54 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.