Results 1 to 3 of 3

Thread: Non-repeating Random No's

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Location
    Aldershot, Hampshire, England
    Posts
    1

    Post

    Apart from being a complete beginner with VB, I would like to know how to generate a group of 6 random numbers (displayed in 6 labels) with none of those numbers being the same.

    I am already aware of the Int(RND * 49)+1 code line that creates a random number between 1 and 49. How can i get non-repeating numbers in my question above?

  2. #2
    Guest

    Post

    You could try the following:
    Code:
    Dim x As Integer
    Dim y As Integer
    Dim Numbers(1 To 50) As Boolean
    Dim Labels(1 To 6) As Integer
    Dim TempInt As Integer
    For x = 1 To 50
       Numbers(x) = False
    Next x
    x = 0
    Do
       x = x + 1
       TempInt = Int((Rnd * 49) + 1)
       For y = 1 To 50
          If TempInt = y Then
             If Not Numbers(y) Then
                Numbers(y) = True
                Labels(x) = TempInt
                Exit For
             Else
                x = x - 1
                Exit For
             End If
          End If
       Next y
    Loop Until x = 6
    Label1.Caption = Labels(1)
    Label2.Caption = Labels(2)
    Label3.Caption = Labels(3)
    Label4.Caption = Labels(4)
    Label5.Caption = Labels(5)
    Label6.Caption = Labels(6)

    ------------------
    Boothman
    There is a war out there and it is about who controls the information, it's all about the information.

  3. #3
    New Member
    Join Date
    Jan 2000
    Posts
    9

    Post

    Try this, but first put the labels into an array:

    Randomize Timer
    For i = 0 To 5
    again:
    Label1(i).Caption = Int(Rnd * 6) + 1
    For j = 0 To 5
    If i = j Then GoTo skip
    If Label1(i) = Label1(j) Then GoTo again
    skip:
    Next j
    Next i

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width