Shadylamp
Shadylamp

Reputation: 83

Python3 randrange giving same result

So I'm making a small text-game and one of the things I want to do is to get a random number from a range of variables.

For testing purposes I've written a code looks something like this:

slow_speed = random.randrange(10,25)
medium_speed = random.randrange(26, 50)
fast_speed = random.randrange(51, 75)

penalty_list = [slow_speed, medium_speed, fast_speed]

for i in range(3):
    for penalty in penalty_list:
        print(penalty)

The idea is that it would loop over the list 3 times and each time give a different random number for each range.

However, what happens is that I get the same 3 numbers looped 3 times. I figured it was because the seed(time on my computer) is the same when I invoke the function, so I've tried to add a time.sleep() and a random.seed() but that didn't help.

So how can I fix this? Thanks!

Upvotes: 4

Views: 2227

Answers (4)

Alexander
Alexander

Reputation: 109536

Define your speeds as tuple ranges, and then call random.randrange on them. Use * to unpack the arguments in the tuple pairs.

slow_speed = (10,25)
medium_speed = (26, 50)
fast_speed = (51, 75)

penalty_list = [slow_speed, medium_speed, fast_speed]

for i in range(3):
    for penalty in penalty_list:
        print(random.randrange(*penalty))

# Output:
17
43
59
11
38
61
24
35
54

Or do the whole thing as a list comprehension:

>>> [[random.randrange(*penalty) for penalty in penalty_list] for _ in range(3)]
[[21, 27, 73], [10, 47, 72], [10, 35, 55]]

Upvotes: 3

Martijn Pieters
Martijn Pieters

Reputation: 1121634

Think of using a typewriter, and having a monkey bang on the keys. Once the hammers have hit the ink tape and the letters have been imprinted on the paper, those letters are not going to change anymore. You can re-read the resulting gibberish on paper, but the gibberish will stay the same. If you want different gibberish, put more paper in the typewriter and have the monkey bang on the keys again.

random.randrange() is that monkey, and it returns you an int object; that's the paper. Printing those integers in a loop won't change the result, you'll need to go back and have random.randrange() bang on the keys each time in the loop:

for i in range(3):
    slow_speed = random.randrange(10,25)
    medium_speed = random.randrange(26, 50)
    fast_speed = random.randrange(51, 75)

    penalty_list = [slow_speed, medium_speed, fast_speed]

    for penalty in penalty_list:
        print(penalty)

Upvotes: 3

FatmaT
FatmaT

Reputation: 255

for i in range(3):
    slow_speed = random.randrange(10,25)
    medium_speed = random.randrange(26, 50)
    fast_speed = random.randrange(51, 75)
    penalty_list = [slow_speed, medium_speed, fast_speed]
    for penalty in penalty_list:
        print(penalty)

you do not generate random number in every loop, thats the reason.

Upvotes: 1

gtlambert
gtlambert

Reputation: 11961

You need to put the variable declarations inside the for loop. This way, random numbers are generated during each iteration. The following is probably what you are looking for:

import random

for i in range(3):
    slow_speed = random.randrange(10,25)
    medium_speed = random.randrange(26, 50)
    fast_speed = random.randrange(51, 75)
    penalty_list = [slow_speed, medium_speed, fast_speed]

    for penalty in penalty_list:
        print(penalty)

Sample Output

13
31
63
17
26
61
16
46
67

Upvotes: 5

Related Questions