Solved

# how to get multiple random numbers

Posted on 2010-11-29
405 Views
I am trying to write a Slot machine program for a VB2008 class and i am stuck. i need to call three different random colors from my array to make it work but they all are the same, and i'm not sure why. what can i do to make it work what code i have so far is following. Thanks in advance :)

Public Class SlotMachine

Dim Colors As Color() = {Color.Blue, Color.Red, Color.Yellow, Color.Green, Color.Orange}
Dim used As Boolean() = New Boolean(0 To Colors.GetUpperBound(0)) {}

Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles PlayButton.Click

Dim rand As Color

rand = Colors(GetRandomNumber())
SlotLabel1.BackColor = rand

rand = Colors(GetRandomNumber())
SlotLabel2.BackColor = rand

rand = Colors(GetRandomNumber())
SlotLabel3.BackColor = rand

End Sub

' return an unused random number
Function GetRandomNumber() As Integer

Dim randomObject As Random = New Random()
Dim randomNumber As Integer = 0 + randomObject.Next(5)

Return randomNumber ' return index for new flag
End Function ' GetUniqueRandomNumber

End Class
0
Question by:Stewart1206
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 34235577
Change:

Dim randomObject As Random = New Random()

To:

Static randomObject As Random = New Random()

*This will not guarantee that they will all be different though.
0

Author Closing Comment

ID: 34235606
0

Author Comment

ID: 34235608
Thank you that did it!  i'm sure i will need some more help before it's done but that problem is solved THANK YOU
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\Sâ€¦
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlinâ€¦
Starting up a Project
###### Suggested Courses
Course of the Month6 days, 2 hours left to enroll