1

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
1
  • 1
    random.randint() always takes 2 arguments; what is the range of values that are allowed for x? Just like rotation and color, the number of values that are legal for x is not limitless. Commented Apr 21, 2014 at 9:59

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

You can also use randrange ,

Syntax is random.randrange([start], stop[, step])

Example ,

>>> from random import randrange
>>> print randrange(10)
5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.