+ 2
I need help in python pls
Hello everyone, I wanted to create a python program where I would have 2 "objects" or think as you like ... "A" and "B" and I wanted my program to choose one of the two but "A" would have a probability 60% to be chosen while the "B" would have 40%. Does anyone know how I can do this in the simplest way possible to understand?
4 Antworten
+ 4
You could use the function randint from random, generating an int between 1 and 10 (or whatever) to model the percentages.
from random import randint
number = randint(1, 10)
if number <= 6:
do_stuff_with(A)
else:
do_stuff_with(B)
+ 3
There is a choices function in the random module that can take weightings but it return a list (although you can return a list with length of 1.
https://docs.python.org/3/library/random.html#module-random
Just scroll down to "choices".
The "weight" bit is over-my-head so I can't help you any more...maybe you can understand it.
+ 1
I don't have a code because I don't know how to make this type of program so the help request