8

I have 10 things that I want to be printed if they are picked. However each should have a different percentage chance of happening though.

I tried doing the following:

    chance = (random.randint(1,100))
    if chance < 20:
        print ("20% chance of getting this")

The problem is that if I do another one with say, chance <25, if the randint is 10, wouldn't both the chance <25 and chance <20 both run at the same time?

Here is the code I want to have the chance stuff going on after.

print ("You selected Grand Theft Auto")
gta = input ("To commit GTA input GTA")
if gta in ("gta", "Gta", "GTA"):

EDIT:

Alright so I tried this but it keeps giving 3 outputs.

print ("You selected Grand Thief Auto")
    gta = input ("To commit GTA type GTA")
    if gta in ("gta", "Gta", "GTA"):
        chance = random.randint(0,100)
        if chance <= 1:
            print ("You stole a Bugatti Veryron")
        chance = random.randint(0,100)
        if chance <= 5:
            print ("You stole a Ferrari Spider")
        chance = random.randint(0,100)
        if chance <= 10:
            print ("You stole a Audi Q7")
        chance = random.randint(0,100)
        if chance <= 15:
            print ("You stole a BMW X6")
        chance = random.randint(0,100)
        if chance <= 20:
            print ("You stole a Jaguar X Type")
        chance = random.randint(0,100)
        if chance <= 25:
            print ("You stole a Ford Mondeo")
        chance = random.randint(0,100)
        if chance <= 30:
            print ("You stole a Audi A3")
        chance = random.randint(0,100)
        if chance <= 35:
            print ("You stole a Ford Fiesta")
        chance = random.randint(0,100)
        if chance <= 40:
            print ("You stole a Skoda Octavia")
        chance = random.randint(0,100)
        if chance <= 45:
            print ("You got caught!")
Yami
  • 117
  • 1
  • 2
  • 7
  • 2
    Use elif, then only one will be selected. – Max Sep 25 '14 at 23:56
  • since each item has a dif chance...why don't you do a loop and random for each item, if its < its chance then add to a list – Steve Sep 25 '14 at 23:59
  • Wow, this may be the first time I've seen a problem that would easier to write in INTERCAL than in Python. :) – abarnert Sep 26 '14 at 00:02

2 Answers2

10

okay so if you want two mutually exclusive events with one occurring 20% of the time and the other occurring 25% of the time, then

chance = random.randint(1,100)
if chance <= 20:
    print("20% chance of getting this")
elif chance <= 20+25:
    print("25% change of getting this")

if you want them to be independent and not influence each other, you have to generate another random number.

chance = random.randint(1,100)
if chance <= 20:
    print("20% chance of getting this")

chance = random.randint(1,100)
if chance <= 25:
    print("25% change of getting this")
Greg Nisbet
  • 6,710
  • 3
  • 25
  • 65
  • Great explanation, but you've missed one problem in the OP's code. You either need `<=` here, or `randrange(100)` instead of `randint(1, 100)`, because there are only 19 numbers in `[1, 100]` that are `< 20`. – abarnert Sep 26 '14 at 00:04
  • Used this, but got 3 outputs, but the percentages seem accurate. – Yami Sep 26 '14 at 00:20
1

You're code is correct. The way to solve this is to first, add an = inside of your first if-statement, as such:

 if chance <= 20

The next thing that can be done is to add a return statement at the end of your print, as such:

 if (chance <= 20):
      print(#Stuff)
      return

This return statment will finish doing whatever the program is doing, and return to another task, or just be done.

Finally, the last thing to do is to add all of the other increments, as such:

 if (chance <= 20):
      print(#Stuff)
      return
 if (chance <= 25):
      print(#Stuff)
      return

 ...

 if (chance <= #last_number):
      print(#Stuff)
      return

It would be wise to be sure that you are bumping everything up as indicated by the odds that you are look for, as well.

Best of luck.

T.Woody
  • 1,142
  • 2
  • 11
  • 25