-1

I've been trying to figure out how to recreate odds in python for a chest opening game thingy, but the random.choice isn't really considering different odds. The goal I'm trying to achieve is set certain odds for different "rarities", like for example having 50% probability for getting an "Uncommon".

Code example:

import random

skins = {
    'Common':'Knife',
    'Uncommon':'Pistol',
    'Rare':'Submachine Gun',
    'Epic':'Automatic Rifle',
    'Legendary':'Semi-Automatic Sniper Rifle'
}


print("Welcome to N/A")
print("Input 'open' to open a chest.")
n = input("> ")
if n == "open":
    print(random.choice(skins))
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Jayotomix
  • 3
  • 2
  • Because Its a "random" choice, I couldn't assign odds to it. like say Uncommon would be a 50% chance. – Jayotomix Sep 14 '21 at 22:12
  • 7
    What do you mean by "the random.choice isn't really considered odds"? Are you asking how to pick a [_weighted_](https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice) sample of a collection? – Brian61354270 Sep 14 '21 at 22:12
  • 3
    Check out [random.choices](https://docs.python.org/3/library/random.html#random.choices). It exposes a `weights` parameter. – gmdev Sep 14 '21 at 22:14
  • This is exactly what I'm looking for @gmdev . Thanks! – Jayotomix Sep 14 '21 at 22:16
  • Okay, so the only thing I still need to know is how to assign weights to rarities instead of just a numbered item in the list? – Jayotomix Sep 14 '21 at 22:21
  • 2
    Do not edit "solved" into titles. The only way you mark a question solved is by clicking the checkbox next to an answer. If no adequate answer exists, add one yourself with the "Add An Answer" button. If the system makes you wait before accepting a solution, then leave the question marked as unsolved until that timeout has ended. See [Is it OK to add SOLVED to the title of a question?](https://meta.stackexchange.com/questions/116101/is-it-ok-to-add-solved-to-the-title-of-a-question) on [meta.se]. – Charles Duffy Sep 14 '21 at 22:27
  • (Stack Overflow is not a discussion forum, so our rules are not discussion-forum rules. Our goal is to build a knowledge base, not to help people 1-on-1, so our rules and procedures are oriented differently from what you might be accustomed to from sites with different goals). – Charles Duffy Sep 14 '21 at 22:31
  • Does this answer your question? [A weighted version of random.choice](https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice) – Peter O. Sep 15 '21 at 03:01

2 Answers2

0

A simple solution is to pick a random number between 0 and 100 and check which percentile it gets within.

import random

skins = {
    'Common':'Knife',
    'Uncommon':'Pistol',
    'Rare':'Submachine Gun',
    'Epic':'Automatic Rifle',
    'Legendary':'Semi-Automatic Sniper Rifle'
}

chance = random.randint(0, 100)
if chance < 50:
    print(skins["Common"])
elif chance < 80:
    print(skins["Uncommon"])
elif chance < 95:
    print(skins["Rare"])
elif chance < 99:
    print(skins["Epic"])
else:
    print(skins["Legendary"])

The first if has 50% chance of executing, the second 30%, the third 15%, the fourth 4% and the else has 1 % chance of executing (assuming linear perfect distribution of randint).

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
0

My advice is to use Random choice() method since it allows you to make a list where you can specify probability for each element. https://www.w3schools.com/python/ref_random_choices.asp

from random import choices
skins = {
    'Common':'Knife',
    'Uncommon':'Pistol',
    'Rare':'Submachine Gun',
    'Epic':'Automatic Rifle',
    'Legendary':'Semi-Automatic Sniper Rifle'
}
weights = [0.4, 0.25, 0.2, 0.1, 0.05]
print("Welcome to N/A")
print("Input 'open' to open a chest.")
n = input("> ")
if n == "open":
    print(choices(list(skins.items()), weights))
Robxon
  • 196
  • 4