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,560 points

edited Dec 14, 2018 by Nymeria 1,411 views

1 answer to this question.

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,090 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, 2019 in Python by Radhika
2,215 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, 2019 in Python by Kiran
952 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, 2019 in Python by Nymeria
• 3,560 points
2,469 views
0 votes
1 answer

how do i print only the last two letters in a string using regular expression in python?

$ to match the end of the ...READ MORE

answered Mar 15, 2019 in Python by Waseem
• 4,540 points
889 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,358 views
0 votes
1 answer
0 votes
1 answer

What's the right way to concatenate files and appending the New Line character in Python? Text or binary mode?

It seems like you're using the wrong ...READ MORE

answered Nov 13, 2018 in Python by Anirudh
• 2,090 points

edited Dec 14, 2018 by Anirudh 1,532 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP