Reputation: 70
How to simulate the roll of an UNFAIR 6-sided die. Instead of each side having an even chance of coming up (1/6 = 16.7%), the middle numbers should be favored. There should be a 20% chance of rolling a 2, 3, 4, or 5, and only a 10% chance of rolling a 1 or a 6. Thanks
Upvotes: 1
Views: 5154
Reputation: 11
import numpy as np
die_roll = np.random.choice(np.arange(1, 7, 1), p = [0.1, 0.2, 0.2, 0.2, 0.2, 0.1])
Upvotes: 1
Reputation: 5414
You can do it lots of ways. This is one of the easier ones :
from random import choice
options = [1, 2, 2, 3, 3, 4, 4, 5, 5, 6]
result = choice(options)
print(result)
There are 10 values. The percentage of probability of getting 1 is (1/10)*100 = 10%, the percentage of the probability of getting 2 is (2/10)*100=20%......
Upvotes: 3
Reputation: 39082
Another possibility:
import random
result = random.choices([1, 2, 3, 4, 5, 6], weights=[10, 20, 20, 20, 20, 10])[0]
See the documentation.
Upvotes: 6
Reputation: 573
If you've got numpy installed, you could use numpy.random.choice
to sample with a given probability distribution.
import numpy as np
values = [1, 2, 3, 4, 5, 6]
probs = [0.1, 0.2, 0.2, 0.2, 0.2, 0.1]
sample = np.random.choice(values, p=probs)
Upvotes: 3