Terms of Agreement:
By using this code, you agree to the following terms...
You may use
this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this code from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
' Name: Non-Repeating Random Number Function
' Description:This is the function version of Kevin Lawrence's Non Repeating Random Number Generator submission, which uses a button and message box. This will output the numbers into an array
' By: Matt DeKok
' Inputs:The max number
' Returns:An array from 0 to the max number
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=69650&lngWId=1'for details.'**************************************
'This will generate an array of non-repeating random numbers 0 to intMax
Private Sub RandomList(ByVal intMax As Long)
Dim High() As Long, Nums() As Long, iMax As Long
'Holds the intMax variable minus 1
iMax% = intMax% - 1
'An array for holding the highest number to choose
ReDim High(iMax%) As Long
'The new array
'The High array will hold a list of sequential numbers from 0 to intMax - 1.
'These numbers will be inserted randomly into the new array.
For i% = 0 To iMax%
High(i%) = i%
'Work backwards from iMax% to 0
For i% = iMax% To 0 Step -1
'Choose a random array index
Chosen% = Int(i% * Rnd)
'Insert an element from High into the new array
Nums(iMax% - i%) = High(Chosen%)
'Replace the chosen element from High with a new number
High(Chosen%) = High(i%)
'Replace existing (or not) TheNums array with the new array
TheNums = Nums
'This will scramble the specified ListBox
Private Sub ScrambleList(ByVal lList As ListBox)
Dim arrList() As String
'Resize the array to the size of the list and subtract 1
'because the index is 0 based
ReDim arrList(lList.ListCount - 1)
'Retrieve an array of random numbers to set the order
'of the list
'Randomly insert the items into the arrList array
For i% = 0 To UBound(arrList)
arrList(i%) = lList.List(TheNums(i%))
'Return the randomized list items to the list
For i% = 0 To UBound(arrList)
lList.List(i%) = arrList(i%)
Randomize and Rnd will repeat numbers. This makes a list of them and begins randomly transfering them from one array to the other and then finally transfers the finished array to the one shown in Global Declarations ("Dim TheNums()") (If this comment was disrespectful, please report it.)
Yes, without Randomize it is useless. It's better to work with Longs instead of Ints and way better than Variants like High() & Nums(). And who knows what TheNums is? Why bother going backwards (step -1)? (If this comment was disrespectful, please report it.)
Randomize sets the seed for the next random number based on the time, you should not be doing this inside the loop. Move the randmize outside the loop else there's a good chance you're actually generating the same number a few times round the loop. Once you've set the seed rnd will generate a number based on the previous rnd result so you're more likely to have a better distribution. (If this comment was disrespectful, please report it.)
I really don't follow this code. After your call of Randomlist there are two FOR loops preceded by the comment "Randomly insert the items in to the arrlist array" but there is nothing random about it. It takes the items out of the Listbox in sequence then in the 2nd loop it puts them back in exactly the same order??? (If this comment was disrespectful, please report it.)
If you TRY it, you'll see that it SHOULD work. It did for me anyway.
Here is an explanation:
RandomList will scramble the index numbers and place their corresponding list items into an array. Then it returns them to the listbox in that random order. (If this comment was disrespectful, please report it.)
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.)