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 101 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 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
39 views
0 votes
1 answer
0 votes
0 answers

How can I write a program to add two numbers using functions in python?

Is there a built-in function to add ...READ MORE

3 days ago in Python by Waseem
• 1,870 points
6 views
+2 votes
2 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 14,960 points
2,364 views
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
• 920 points
18 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.