Start New Discussion within our Software Development Community

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..

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

Randomize

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.