Okay.. Lets say I have ten labels and I want a random number between one through ten in each label.. But in the labels I dont want to have duplicates so that way in labels 1 through 10 they will have an integer between 1 and 10.. But randomized..

9 Years
Discussion Span
Last Post by vb5prgrmr

Hmmm... Something along a card shuffle algorithm...

Option Explicit

Dim NumArray() As Integer

Private Sub Form_Load()
Dim ForLoopCounter As Integer, Temp As Integer, HoldValue As Integer
ReDim NumArray(9) As Integer


For ForLoopCounter = 0 To 9
  NumArray(ForLoopCounter) = ForLoopCounter
Next ForLoopCounter

For ForLoopCounter = 0 To 9
  'get random number from 1 to 10
  Temp = Rnd() * UBound(NumArray)
  'set labels caption
  Label1(ForLoopCounter).Caption = NumArray(Temp)
  'check to make sure we need to continue
  If UBound(NumArray) = 0 Then Exit For
  'now swap values
  HoldValue = NumArray(UBound(NumArray))
  NumArray(UBound(NumArray)) = NumArray(Temp)
  NumArray(Temp) = HoldValue
  'then get rid of used number
  ReDim Preserve NumArray((UBound(NumArray) - 1)) As Integer
Next ForLoopCounter

End Sub

Good Luck

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.