Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random question
#1
Hi,

Suppose a function that runs over and over again.
At a certain point it calls another function with an argument 'A' or '.B'
Randomly 70% of the time with 'A', 30 % of the time with 'B'. (or any other combination)
The way i implemented this is to make a tuple with the distribution and do a random.choice().
A70pct = ('A','A','A','A','A','A','A','B';'B','B')
c = random.choice(A70pct)
The upside : it is very simple and effective. This will also allow for a third argument 'C', with e.g. a 50-30-20 distribution.etc.
The downside: if i need more than one distribution, like 60-40 or... i need to hard code these tuples.
I could write a function, that creates a tuple on the fly, does the .choice() and returns the result.
But that code then needs to run over and over again, the hard coding beingmore efficient, if there are only a few distributions.

Question: there may be a better way of doing this, an exotic function i never heard of ?

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#2
Look at https://stackoverflow.com/questions/4265...stribution

In your case I think using random.choices is the optimal solution
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jul-22-2020, 07:31 AM)buran Wrote: Look at https://stackoverflow.com/questions/4265...stribution

In your case I think using random.choices is the optimal solution

OK, this answers the question.
Thanks
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question about the Random Module Exsul 1 1,964 Mar-13-2019, 02:06 AM
Last Post: ichabod801
  Newbie question: random Truman 8 4,835 Dec-19-2017, 09:27 PM
Last Post: Truman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020