Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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.

Sun Certified Web component Developer,

Microsoft Certified Solution Developer .NET,

Software Engineer

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...