Stanley Roy Chilton
Stanley Roy Chilton

Reputation: 71

Randrange dependent on variable - Python

Im trying to make random co-ords for a object spawning onto a canvas(The canvas is the size of the users screen so the range is ever changing.)

random.randrange(12.5, int(resX-12.5))

This is the line of code resX being the variable that stores the size of the users screen resolution, 12.5 being the radius of the shape being spawned.

The error i'm getting is that the resX part isn't an integer ValueError: non-integer arg 1 for randrange()

Im not sure if theres a way to use a variable within the random line?

Upvotes: 1

Views: 664

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

You need random.uniform for getting a random float number between a specific range:

>>> resX = 100
>>> random.uniform(12.5, int(resX-12.5))
55.797969682558296

Note that since you can pass a float number as the range into uniform() you might don't need to call the int function on resX-12.5 but since this change may affect the result, it depends on what you want.Otherwise you can do it like following:

>>> random.uniform(12.5, resX-12.5)
66.1303523838728

Upvotes: 2

Related Questions