user2423223
user2423223

Reputation: 115

random.randint function isnt working the way i would like it to

Whenever I run this program (not a serious program just messing around getting an idea of things) the damage from the monster (monster[1]) just repeats the same number over and over again. It randomizes once then just repeats it. I understand WHATS happening but I don't understand WHY it is. Here is the code (I'm sorry I don't know if I'm making my question clear or not...makes sense in my head lol)

EDIT - I would like the monster[1] function to randomize each time but I don't know how to do it without writing more code (i.e damage=random.randint(5, 15) health-=damage). If I write it like that it works fine. But I dont want to write that specially if I have a series of 10 different monsters. I would like to just call upon each monster and have it run by itself if that makes sense.

EDIT#2: Is it possible to create a tuple within a list since tuples are immutable (i.e monster=[100, (random.randint(5, 15)), random.randint(15, 30)] this code doesn't work I've tried it already but just wondering if it would be possible.

import random
monster = [100, random.randint(5, 15), random.randint(15, 30)]
health = 200
a = 1

print "you run into a monster"
while a == 1:
    if monster[0] <= 0:
        print "monster is dead"
        print "you get, " + str(monster[1]) + " exp"
        a += 1
    elif monster[0] >= 0:
        att = random.randint(5, 15)
        monster[0] -= att
        health -= monster[1]
        print ("you attack the monster and do, " + str(att) + " damage"
               " the monster does," + str(monster[1]) + " damage to you")
        print "you have, " + str(health) + " health left"
        print "monster has, " + str(monster[0]) + " health left"

Upvotes: 3

Views: 1789

Answers (5)

sissi_luaty
sissi_luaty

Reputation: 2889

When you call your line:

monster = [100, random.randint(5, 15), ...]

the function random.randint(5, 15) is executed and a specific value is stored in the list.

I'm going to present one more alternative to do what you want, through the storage of the function as a string; you can write:

monster = [100, "random.randint(5, 15)", ...]

then when you want to get a new random integer you call:

r=eval(monster[1])

and that gives you a different integer each time you call it, because the function is re-evaluated.

(notice that if you want to re-use a random value, for instance to print it, you must store it in a temporary variable, "r", and then use str(r), instead of str(eval(monster[1])), as the former way prints the result of the call stored in "r", and the later way re-evaluates again the function).

eg.

    tmp = eval(monster[1])
    health -= tmp
    print ("you attack the monster and do, " + str(att) + " damage. the monster does," + str(tmp) + " damage to you")

Upvotes: 0

DHandle
DHandle

Reputation: 402

monster[1] get assigned a random integer on the second line of your code. It then is stored in the list monster as an integer. That integer is going to remain the same. You would have to rerun the random.randint(5, 15) call to get a new "damage" number or new exp amount.

Though I think it may be beyond the scope of what you are working on here, you may want to look into creating a class monster instead of using an array, and then having a method that generates these random integers each time it attacks.

Here is an example monster class that may give you the idea of how it works.

class monster():
    def __init__(self,health):
        self.health = health

    def attack(self):
        return random.randint(5,15)

You can then create a new monster as follows.

monster = monster(100)

See how much damage it does as follows.

monsterDamage = monster.attack()
health -= monsterDamage
print "The monster does " + str(monsterDamage) + " damage to you."

And lower the monster's health as follows.

att = random.randint(5,15)
monster.health -= att

Upvotes: 2

Tim Peters
Tim Peters

Reputation: 70602

Well, you gave monster[1] a value only once, in the 2nd line:

monster=[100,random.randint(5, 15), random.randint(15, 30)]

That line is executed only once, so "of course" monster[1] is always the same. If you want monster[1] (and/or monster[2] too) to change, you need to give it a new value inside your loop. You did that for att! You need to do something similar for all the other things you want to see change :-)

Upvotes: 1

TerryA
TerryA

Reputation: 59974

monster=[100,random.randint(5, 15), random.randint(15, 30)]

When you define this list, it will be the same throughout. The values will not change every time you do monster[1].

To fix this, you could do something like:

monster=[100, random.randint, random.randint]

And when you go to call it:

monster[1](15, 15)

Just an option. Although a dictionary could be better here.

Upvotes: 1

Alex Gittemeier
Alex Gittemeier

Reputation: 5373

This line:

monster=[100,random.randint(5, 15), random.randint(15, 30)]

saves 100 and two random numbers once. For example, this may be the representation of monster at runtime:

monster=[100, 11, 26]

And these numbers don't change, because the assignment happens only once. (eg. outside the loop)

Upvotes: 1

Related Questions