Generating Random Numbers in VB in Applications & Coding Posted January 28, 2009 My first "help" thread here :) I am wrting a Visual Basic application that will need to use an array with anywhere from 2 to 10 elements that contain unique numbers within a range generated randomally. I'm using VS 2008 for this. The code I got for this in a test project I'm doing until I can figure out how its done: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Array to be used Dim randomnumbers(9) As Integer 'String that well contain the values to display in the messagebox Dim messagestring As String = "" 'Cycles through each element in the array For I As Integer = 0 To randomnumbers.Count - 1 'Assigns the current I element a random number between the range of 1 and 10 randomnumbers(I) = CInt(Int((10 * Rnd()) + 1)) 'Only happens on the 2nd or any other go-around since there well never be a first answer that is the same when the others have not be given values. If I > 0 Then 'Compares the current element to the last one. If equal, decrease I so the same element is used in the next go around. If randomnumbers(I) = randomnumbers((I - 1)) Then I -= 1 Else 'Appends the current element to the string to be displayed at the end when it is unique from previous elements messagestring &= randomnumbers(I) & " " End If Else 'Assigns the first element to the string to be displayed at the end of the procedure. messagestring &= randomnumbers(I) & " " End If Next MessageBox.Show(messagestring) End Sub End Class The code above does generate numbers between the ranges of 1-10 for the ten elements of the array, but despite the if condition I put in to redo the element if it equals the last element, it ends up giving me two or more elements with the same number just about everytime I execute the code.