Hello.
We are currently trying to create a program in the following order.
I'm going to proceed, but I don't know what to do with the probability of applying random numbers.
I know how to generate simple random numbers, but I would appreciate it if you could tell me how to apply the probability.
Currently, I would like to do it through Python.
Thank you.
python random-generation probability
From Python 3.6, random.choice
function can be selected by giving weights
.
random.choice (range (1, 6), weights=[1,1,1,1]) # All choices with the same probability
random.choice (range(1, 6), weights=[1,2,1,1]) #2 is twice as likely as the others
https://docs.python.org/3/library/random.html#random.choices
import time
import random
candidate = ['A', 'B', 'C', 'D', 'E']
while (True):
selected_index = random.randrange(len(candidate))
print(candidate[selected_index])
candidate.append(candidate[selected_index])
time.sleep(1)
© 2025 OneMinuteCode. All rights reserved.