0

I want to make a text-based game where you mine for ores and gems in python, and I want to make it a random choice of what you get when you type 'mine', how do you make it so some of the random choices are more rare then the other?

here is my code so far: `

import random, 
start = raw_input('Welcome to the mining game! Type "Start" to continue. ')
if str.upper(start) == ("START"):
  default_cash = 14
  print "You have %g cash." % default_cash
  choices = raw_input("What would you like to do? Type 'Mine' to dig for ores, or type 'Shop' to go shopping for equipment. ")
  if str.upper(choices) == ("MINE"):
    ores = ["emerald", "ruby", "silver", "gold", "diamods", "nothing"]
    ores_random = random.choice(ores)
    print "you found %s!" % ores_random`
i28v
  • 33
  • 3
  • 1
    Possible duplicate of [Probability distribution in Python](http://stackoverflow.com/questions/526255/probability-distribution-in-python) – Peter O. Mar 15 '16 at 22:59

2 Answers2

0
from numpy.random import choice
draw = choice(ores, number_of_items_to_pick, p=probability_distribution)

You can use choice function of numpy. See that probability_distribution is a sequence in the same order of ores.

Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
0

If you don't want to use anything other than the imports you already have, the easy solution is replacing ores by:

    ores = ["emerald"]*3+["ruby"]*7+["silver"]*10+["gold"]*20+["diamods"]*20+["nothing"]*40

,where the bigger the multiplier the less rare the item. This isn't a good solution however. It would be better solution to build a function where, stating the distribution:

    dist = [3,10,20,40,60,100]
    ores = ["emerald", "ruby", "silver", "gold", "diamods", "nothing"]

, you could do generate a random integer and retrieve the correct item:

    for i in range(1,len(ores)):
            r = random.randint(0,100)
            if dist[i-1]<=r<dist[i]:
                    break
    print "you found %s!" % ores[i]

Obviously if you are willing to use libraries such as numpy you'll get a performance boost.

armatita
  • 12,825
  • 8
  • 48
  • 49