VB icon

A Non-Repeating Random Number Generator

Email
Submitted on: 1/1/2015 2:52:00 PM
By: Kevin Lawrence  
Level: Not Given
User Rating: By 9 Users
Compatibility: VB 5.0, VB 6.0
Views: 16
 
     With this simple, and very fast, routine you can generate a series of non-repeating random numbers. You can select a series of 10 numbers, or a series of a million...It doesn't matter. Can be useful for image fades, deck shuffling, random tip of the day, etc. - It even tells you how long it took to generate the series.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: A Non-Repeating Random Number Generator
' Description:With this simple, and very fast, routine you can generate a series of non-repeating random numbers. You can select a series of 10 numbers, or a series of a million...It doesn't matter. Can be useful for image fades, deck shuffling, random tip of the day, etc. - It even tells you how long it took to generate the series.
' By: Kevin Lawrence
'
' Returns:A popup message stating how many numbers had been mixed up and how long it took.
'
' Side Effects:The larger the series of numbers the more RAM required. Uses arrays.
'**************************************

Private Sub Command1_Click()
'-------------------------------------------------------------
' Produces a series of X random numbers without repeating any
'-------------------------------------------------------------
'Results can be used by using array B(X)
Dim A(10000) ' Sets the maximum number to pick
Dim B(10000) ' Will be the list of new numbers (same as DIM above)
Dim Message, Message_Style, Message_Title, Response
'Set the original array
MaxNumber = 10000 ' Must equal the DIM above
For seq = 0 To MaxNumber
A(seq) = seq
Next seq
'Main Loop (mix em all up)
StartTime = Timer
Randomize (Timer)
For MainLoop = MaxNumber To 0 Step -1
ChosenNumber = Int(MainLoop * Rnd)
B(MaxNumber - MainLoop) = A(ChosenNumber)
A(ChosenNumber) = A(MainLoop)
Next MainLoop
EndTime = Timer
TotalTime = EndTime - StartTime
Message = "The sequence of " + Format(MaxNumber, "#,###,###,###") + " numbers has been" + Chr$(10)
Message = Message + "mixed up in a total of " + Format(TotalTime, "##.######") + " seconds!"
Message_Style = vbInformationOnly + vbInformation + vbDefaultButton2
Message_Title = "Sequence Generated"
Response = MsgBox(Message, Message_Style, Message_Title)
End Sub


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.