## Random number assignment help?

This is the place for queries that don't fit in any of the other categories.

### Random number assignment help?

Hello, I'm currently learning python. I've been using it for around three days or so now, going at it slow. I'm currently attempting to make a simple slot machine game.

I'm using pygame to do all the drawing etc, but I've stumbled across a problem. I'm using a random number which then assigns a different colour to the Icon which gets spins. In other words, I draw 9 circles always in the same place, but then the colour changes depending on the random number.

I've done a little bit of testing, but the colours constantly change, and I don't know how to keep them the same, until the person presses spin again.

Note, my program is very bare as I was going to get it all drawn out then implement the game logic.

Code: Select all
` # Spin Iconsfor i in range(1, 4):    for j in range(1, 4):        randIconColour = random.randrange(5) # Creates a random colour        randColour = [green, purple, yellow, blue, pink]                    pygame.draw.ellipse(screen, randColour[randIconColour], [((85 * i) + (135 * (i - 1))), (117 * j), 80, 80])`

This is the current code for drawing and creating the random colour, all of this is inside a game loop.

Thanks for any help, it would be much appreciated.
Kegorian

Posts: 4
Joined: Tue Jul 02, 2013 5:15 pm

### Re: Random number assignment help?

Hi,

You probably need an 'if' condition to check for currently spinning state.
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1399
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Random number assignment help?

I'm not even up to that bit yet. My problem is that, because the random.randrange is within the game-loop every frame, it draws a different colour, however I need at least 9 random numbers that change one for every icon.

I'm not too good at explaining things, so if you want to the rest of my code, just ask
Kegorian

Posts: 4
Joined: Tue Jul 02, 2013 5:15 pm

### Re: Random number assignment help?

If i understand correctly.
Create a function that makes a single random colour, call that function 9 times to create 9 random colours.
something like this ?
Code: Select all
`import randomdef createSingleColour():    return random.choice(('green', 'purple', 'yellow', 'blue', 'pink'))colours = [createSingleColour() for i in range(9)]print colours`

Code: Select all
`['purple', 'yellow', 'purple', 'blue', 'blue', 'purple', 'purple', 'yellow', 'blue']`
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1399
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Random number assignment help?

This may help. Since I've only been going a few days I haven't came across functions yet. But I'll skip to that part so I know what I'm doing with this code. I'll give it a try next time I have some free time and let you know if it works or not.
Kegorian

Posts: 4
Joined: Tue Jul 02, 2013 5:15 pm

### Re: Random number assignment help?

Ok here is the same code simplified with no functions and a normal loop
Code: Select all
`colours = []for i in range(9):    colour = random.choice(('green', 'purple', 'yellow', 'blue', 'pink'))    colours.append(colour)print colours`

Code: Select all
`['blue', 'blue', 'green', 'blue', 'purple', 'purple', 'yellow', 'yellow', 'green']`
Join the #python-forum IRC channel on irc.freenode.net!

Yoriz

Posts: 1399
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Random number assignment help?

This version is a lot better. Had some trouble but managed to get the other working. However for now at least. I'm going to swap to this one. Thanks for all the help
Kegorian

Posts: 4
Joined: Tue Jul 02, 2013 5:15 pm

### Who is online

Users browsing this forum: codingcodecoder, Google [Bot] and 4 guests