VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Generates unique non repeating random numbers within a range

by N. K. Velu (2 Submissions)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 4th October 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Generates unique non repeating random numbers within a range

Rate Generates unique non repeating random numbers within a range



button.  Unique random numbers between 1 and 15 will be displayed.
--------------------------------------------------------------------------------

Private Sub cmdDisplay_Click()
Static count As Integer
Dim intNum As Integer

ReDim Preserve arrNum15(1 To 15) As Integer
count = count + 1
Randomize
intNum = Int((15 * Rnd) + 1) ' a random number between 1
                             ' and 15 is generated
                             
If count <> 1 Then

'This loop compares all the previous random numbers to the currently
'generated one to see whether it already exists.  if so a new one is generated.

    For i = 1 To count - 1
        Do Until arrNum15(i) <> intNum
            If arrNum15(i) = intNum Then
                intNum = Int((15 * Rnd) + 1)
                i = 1 'to start checking from array element 1
            End If
        Loop
    Next i
End If
arrNum15(count) = intNum ' assign value to the appropriate array element
If count = 15 Then
    count = 0
    Print "*************"
    Exit Sub
End If
Print intNum ' to print the numbers on the form
End Sub

Download this snippet    Add to My Saved Code

Generates unique non repeating random numbers within a range Comments

No comments have been posted about Generates unique non repeating random numbers within a range. Why not be the first to post a comment about Generates unique non repeating random numbers within a range.

Post your comment

Subject:
Message:
0/1000 characters