randomizing

Hi there;

I have a simple question: i'd like to randomize numbers (eg between 1-10) and like to check whether the generated number has already generated previously, and if so, then generate a new random number, and so on, until each number has been chosen once. In this case, the randomization should restart, reseting all previously chosen numbers.

Can anyone post me a code sample

thnx for helping me out;




Answer this question

randomizing

  • Gpg

    thnx to all, finally i made it with your help... :)

  • Artil

    there are a few examples. Check these:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1049989&SiteID=1

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=939254&SiteID=1

    I think the first link is what you are really after



  • Participant

    You are looking for a shuffle sort. Here is some sample code in a Console application that randomly sorts numbers from 1-10 without duplicates:

    Dim x, mix, temp As Integer
    Console.WriteLine("Shuffle Sort - Random numbers without duplicates")
    Console.Write("How many numbers (up to 20) do you want to shuffle ")
    Dim rndarray(10) As Integer
    Dim randnum As Random = New Random()
    For x = 1 To 10
    rndarray(x) = x
    Next x
    Console.Write(vbCrLf & "10 random numbers w/o duplicates.")
    Console.WriteLine("Enter to repeat, any other key to stop." & vbCrLf)
    Do
    For x = 1 To 10
    mix = randnum.Next(1, 11)
    temp = rndarray(mix)
    rndarray(mix) = rndarray(x)
    rndarray(x) = temp
    Next x
    For x = 1 To 10
    Console.Write("{0} ", rndarray(x))
    Next x
    Console.WriteLine(vbCrLf)
    entry = Console.ReadKey(True).KeyChar
    Loop While entry = Chr(13)
    Console.WriteLine()



  • randomizing