471,594 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 471,594 developers and data experts.

Non-repeating random numbers

8,435 Expert 8TB
This is a simple VB6 function to generate random numbers in the specified range, without repeating any numbers.

New, and only briefly tested. Use at your own risk. :)

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private UsedNumber As New Collection
  4. Private Const MaxAttempts As Long = 150
  5. Private Const Zero As Long = 0
  6. Private Const One As Long = 1
  7.  
  8. Public Function RandomNonRepeatingBetween(ByVal LowerLimit As Long, ByVal UpperLimit As Long) As Long
  9.   ' Generate a pseudo-random number between the specified bounds.
  10.   ' Never return one which has already been used.
  11.   ' Don't forget to use Randomize at least once before calling
  12.   ' this function (but not EVERY time, if calling repeatedly).
  13.  
  14.   ' The record of used numbers will be cleared only when the
  15.   ' caller specifically requests it by passing zero as both the
  16.   ' upper and lower bounds.
  17.  
  18.   ' If it is not possible to satisfy the request (for instance,
  19.   ' if all numbers in the requested range have already been
  20.   ' used, then error 17 ("Can't perform requested operation")
  21.   ' will be raised. Caller should allow for this and handle
  22.   ' it as appropriate.
  23.  
  24.   ' Note, for the moment it will simply try up to 150 times, I
  25.   ' haven't worked out the code yet to do a proper check for
  26.   ' whether any numbers are left in the range.
  27.  
  28.   ' If lower/upper limit are backward, swap them around.
  29.   Dim Temp As Long
  30.   Dim Attempts As Long
  31.  
  32.   If LowerLimit > UpperLimit Then
  33.     Temp = LowerLimit
  34.     LowerLimit = UpperLimit
  35.     UpperLimit = Temp
  36.   End If
  37.  
  38.   ' Clear memory if caller requests it.
  39.   If LowerLimit = Zero And UpperLimit = Zero Then
  40.     Do While UsedNumber.Count > Zero
  41.       UsedNumber.Remove One
  42.     Loop
  43.   End If
  44.  
  45.   On Error Resume Next
  46.   Do
  47.     Temp = LowerLimit + Rnd * (UpperLimit - LowerLimit)
  48.     UsedNumber.Add Temp, Format(Temp)
  49.     If Err.Number = Zero Then
  50.       RandomNonRepeatingBetween = Temp
  51.       Exit Do
  52.     End If
  53.     Err.Clear
  54.     Attempts = Attempts + One
  55.   Loop While Attempts < MaxAttempts
  56.   Err.Clear
  57.   On Error GoTo 0
  58. End Function
Jul 3 '07 #1
0 8897

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Mario | last post: by
32 posts views Thread by Adrian Herscu | last post: by
8 posts views Thread by Bern McCarty | last post: by
14 posts views Thread by Patrick Kowalzick | last post: by
399 posts views Thread by =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post: by
12 posts views Thread by puzzlecracker | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.