Home > OS >  Need Probability function in Python
Need Probability function in Python

Time:11-01

I’m currently learning a couple of programming languages, but I need to make something like a function that works as the following:

Imagine you’ve a bag where you know you’ve 5 blue balls, 2 yellow balls, 1 pink ball. And you want to take one ball out of the bag and check it’s color. So the ideia is that the array should be [blue, yellow, pink] and not [blue, blue, blue…, yellow, yellow, pink]

Is it possible?

I’m still learning the basics.

CodePudding user response:

Example using random.choices

import random

a = ["blue", "yellow", "pink"]

print(random.choices(a, weights=[5/8, 2/8, 1/8]))

CodePudding user response:

You can try this and can modify any number of drawings you may need

from random import choices

colors = ['Blue', 'Yellow', 'Pink'] # All color choices
prob_dist = [0.625, 0.25, 0.125] # Probability Distribution of the colors in order
num_choice = 1 # Number of colors you want to pick at a time

draw = choices(colors, weights=prob_dist, k=num_choice)
print(draw)
  • Related