473,224 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,224 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
  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
  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).
  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.
  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.
  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.
  28.   ' If lower/upper limit are backward, swap them around.
  29.   Dim Temp As Long
  30.   Dim Attempts As Long
  32.   If LowerLimit > UpperLimit Then
  33.     Temp = LowerLimit
  34.     LowerLimit = UpperLimit
  35.     UpperLimit = Temp
  36.   End If
  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
  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 9224

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

Similar topics

by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.