I am trying to run a random variable multiple times

Question:

I am very new to python, and am running into an issue I don’t fully understand. I am trying to get a random variable to run multiple times, but for some reason it just returns the same random value x times.

I am not entirely certain what to try aside from the code I have already done.

lowTreasureList = "50 gold", "Healing Potion", "10x Magic Arrows", "+1 Magic Weapon"

def ranLowLoot(lowLootGiven):
    # This function returns a random string from the passed list of strings.
    lootIndex = random.randint(0, len(lowLootGiven) - 1)
    return lowLootGiven[lootIndex]

lowLoot = ranLowLoot(lowTreasureList)

treasureSelection = int(input())

if treasureSelection == 1:
    numLowTreasure = int(input('How many treasures? '))
    for i in range(numLowTreasure):
        ranLowLoot(lowTreasureList)
        print(lowLoot)

When I do this I get the same random treasure (numLowTreasure) times, but I am trying to get it to select a new random treasure each time.

Asked By: Joshua Farmer

||

Answers:

The problem is that in the main loop you are discarding the result of the call to ranLowLoot(). As a minimal fix, in the main loop assign the result of that function call. Use:

lowLoot = ranLowLoot(lowTreasureList)

rather than simply:

ranLowLoot(lowTreasureList)

As a better fix, ditch your function completely and just use random.choice() (which does what you are trying to do, with much less fuss):

import random

lowTreasureList = ["50 gold", "Healing Potion", "10x Magic Arrows", "+1 Magic Weapon"]

treasureSelection = int(input())

if treasureSelection == 1:
    numLowTreasure = int(input('How many treasures? '))
    for i in range(numLowTreasure):
        lowLoot = random.choice(lowTreasureList)
        print(lowLoot)
Answered By: John Coleman

If you haven’t already, it will help to read the documentation on the random module.

There are three alternatives to random.randint that are more suited to your purpose:

  • random.randrange(start, stop, [step]): step is optional and defaults to one. This will save you the len(...) - 1 you are using to get lootIndex, since stop is an exclusive bound.
  • random.randrange(stop): uses a default start of zero and default step of 1, which will save you passing 0 as your start index.
  • random.choice(seq): you can pass your function’s parameter lowLootGiven to this as seq, which will save you from using indices and writing your own function entirely.

As for why you’re getting the repeated treasure, that’s because you aren’t updating your variable lowLoot in your for loop. You should write:

for i in range(numLowTreasure):
    lowLoot = ranLowLoot(lowTreasureList)
    print(lowLoot)

Last thing I want to say is that python is nice for writing simple things quickly. Even if there was some bigger context that you were writing this code in, I might have written it like this:

lowTreasureList = ("50 gold", "Healing Potion", "10x Magic Arrows", "+1 Magic Weapon")

if int(input()) == 1:
    for i in range(int(input('How many treasures? '))):
        print(random.choice(lowTreasureList))

Using the round parentheses around the tuple declaration like I did isn’t necessary in this case, but I like to use it because if you want to make the tuple declaration span multiple lines, it won’t work without them.

Reading documentation on standard libraries is something I almost always find helpful. I think Python’s documentation is great, and if it’s bit too much to digest early on, I found tutorialspoint to be a good place to start.

Answered By: starball
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.