Arrays

mahboula

Newcomer
Joined
Oct 20, 2003
Messages
2
Location
canada
Hi, i am trying to randomly display data from an array. However, there is repetition with the data displayed. How can i display the numbers randomly without repetition. For example, i am displaying "a", "b"....J randomly, but sometimes, the letter "a" is displayed more than once. Here is what i ahve so far.

Function Names() As String


Dim namesArray() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
Dim intRandom As Integer = Int(Rnd() * 10 + 1)
Return namesArray(intRandom - 1)

Any help would be greatly appreciated. Thanks.
 
Use the Random class in .NET instead of rnd().

Dim r As New Random()

you can aslo seed the constuctor with Ticks from the TimeDate.
 
Random() class doesn't guarantee preventing the repetition since random means random^^.
To prevent repetition, you have to program.
Whenever you generate new random number, you have to compare previous random number. If alreay shown, find another random until finding new random number.
 
I did a simular thing once but in Pascal. Here is how I did it.


Use two arrays. Random a number and pick out the element from the first array and display it. Delete it from the first array and move it into the 2nd array. Do this until the first array is empty, move all elements back to the first array. Clear 2nd array and start again.

This way you'll never get dublicates

Hope it helps
/Farek
 
Back
Top