Reputation: 19
so i am making a basic game in python (on a raspberry pi) and for a x coordinate i need it to be random and it wont work when i use random.randint() and therefore i dont know what to do:
as said i need x to be a random number and y to stay as -2
def getNewPiece():
shape = random.choice(list(PIECES.keys()))
newPiece = {'shape': shape,
'rotation': random.randint(0, len(PIECES[shape]) - 1),
'x': random.randint(),
'y': -2 ,
'color': random.randint(0, len(COLORS)-1)}
return newPiece
Upvotes: 1
Views: 1746
Reputation: 8390
You can also use randrange
,
Syntax is random.randrange([start], stop[, step])
Example ,
>>> from random import randrange
>>> print randrange(10)
5
Upvotes: 0
Reputation: 32189
You actually need to pass in your range from which the random int needs to be generated.
random.randint(1,10)
where 1
and 10
are included in the random picking. Therefore, the method above will return any integer between 1 & 10 (inclusive). For a different range, just specify different start and end parameters.
Documentation: random.randint
Upvotes: 2