Important alert: (current site time 4/17/2014 11:24:07 AM EDT)
 

VB icon

Non-Repeating Random Number Function

Email
Submitted on: 11/27/2007 11:03:56 AM
By: Matt DeKok  
Level: Intermediate
User Rating: Unrated
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access, VBA MS Excel
Views: 9975
author picture
(About the author)
 
     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
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Non-Repeating Random Number Function
'**************************************
Dim TheNums() As Long
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. 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.
  2. 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.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. 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
 ReDim Nums(iMax%)
 Randomize
 '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%
 Next
 '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%)
 Next
 'Replace existing (or not) TheNums array with the new array
 TheNums = Nums
End Sub
'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
 RandomList lList.ListCount
 'Randomly insert the items into the arrList array
 For i% = 0 To UBound(arrList)
 arrList(i%) = lList.List(TheNums(i%))
 Next
 'Return the randomized list items to the list
 For i% = 0 To UBound(arrList)
 lList.List(i%) = arrList(i%)
 Next
End Sub


Other 20 submission(s) by this author

 


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

11/20/2007 3:50:26 AMBrian Ail

is it more efficient than using Randomize?
(If this comment was disrespectful, please report it.)

 
11/20/2007 1:26:06 PMMatt DeKok

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

 
11/20/2007 8:16:21 PMPaul Bahlawan

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

 
11/23/2007 2:02:07 PMMatt DeKok

As I said above the code, this is the "function version" of Kevin Lawrence's script. He wrote the code for a response to a command_click. The (step -1) is the same as what he had in his.

As for the longs instead of integers and variants, you're right. I was also simply doing what he did to begin with.
(If this comment was disrespectful, please report it.)

 
11/26/2007 3:07:48 PMIan

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

 
11/29/2007 10:06:18 AMIan

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

 
12/6/2007 12:40:34 PMMatt DeKok

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

To post feedback, first please login.