czedarts
czedarts

Reputation: 51

Python random.randrange producing everything but the step

When using this following code:

import random
sticks = 100
randomstep = random.randint(1, 6)
expertmarbles = random.randrange(1, sticks, 2**randomstep)

the output is producing everything excluding the step, so for example i would like this to output a random from these numbers: 2,4,8,16,32,64. However it will output everything but these numbers. Can anyone offer any advice, the reason i'm using variables here is because the amount of sticks will decrease.

Upvotes: 2

Views: 228

Answers (2)

Abhijith Asokan
Abhijith Asokan

Reputation: 1875

You can try this

def myRand(pow_min,pow_max): 
    return 2**random.randint(pow_min,pow_max)

I would suggest you to use this instead of random.choice, which requires you to generate a list, which is unnecessary.

Upvotes: 2

bphi
bphi

Reputation: 3185

Instead of using random.randrange you could use random.choice (docs):

import random
min_power = 1
max_power = 6
print(random.choice([2**i for i in range(min_power, max_power + 1)]))

Upvotes: 3

Related Questions