473,394 Members | 1,869 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,394 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 9267

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

Similar topics

5
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...
3
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...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
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...
14
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; };
11
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...
2
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...
0
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...
399
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...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.