I want to know how to apply probabilities when generating random numbers.

Asked 2 years ago, Updated 2 years ago, 28 views

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

2022-09-21 11:56

2 Answers

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


2022-09-21 11:56

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)


2022-09-21 11:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.