Solved

how to get multiple random numbers

Posted on 2010-11-29
3
398 Views
Last Modified: 2013-11-27
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
Comment
Question by:Stewart1206
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
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

by:Stewart1206
ID: 34235606
answered it correctly on first reply :D
0
 

Author Comment

by:Stewart1206
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now