Learn how to a build a cloud-first strategyRegister Now


Random number with a twist....

Posted on 2006-06-07
Medium Priority
Last Modified: 2010-04-30

I need to generater a random order number between 50000 and 60000. No problem I hear you say! However, previous order numbers are stored in a data grid (along with other information) so I don't need a random number to be a duplicate of an existing one.

Does anyone have any idea of how this can be achieved?


Question by:wildarmsdave
  • 3
  • 3

Expert Comment

ID: 16854452
What you can do is generate an array storing the integers 0 - 60,000.  So index '0' would have '0', etc.  So it stores all the values you need.  Then, generate a random number from 0 - size of array (60,000).  When it's used, remove that entry from the array.  Ie, if you hit the random number 50, then remove arr(50).  Then, next time you generate a random number from 0 - size of the array (59,999) it will get another random number from 0 - 60,000 excluding the one you've already used.  In a sense,  all you're doing is randomly 'moving' numbers over from one place to another.  But it is truly random and should work for you.


Expert Comment

ID: 16854481
Sorry i forgot to include the fact that your number is between 50,000 and 60,000.  In that case, your array will be size 10,000 and arr(0) will be 50,000 and arr(10,000) will be 60,000.  But the algorithm is still the same.  Generate random #'s from 0- size of the array and remove those array entries as you go.

Good luck.


Author Comment

ID: 16855834
I think this is fine but what if I exit the program and restart it? The array will be cleared and I could possible end up with a duplicate number????
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Accepted Solution

prosh0t earned 1000 total points
ID: 16855998
What it comes down to is that you need access to either :
1. what #'s are available  or
2. what #'s are already used

#2 is obviously already in the database.  It's just easier to have true 'randomness' if you had #1 available to you.  

If you only wanted to use #2 you could do something like this:
-gen. a random # from 50,000 to 60,000
-check if it exists.  If it doesn't, then use it.  If it does, then keep incrementing it until we hit one that doesn't exist.

It's a sort of 'hash' algorithm.  But... (correct me if I'm wrong) i don't believe that gives truly random results, which is why I mentioned method #1.  

Another way if you wanted to go the route of using #2 is, you would keep generating random #'s until you hit one that wasn't used.  But that will slow down as more #'s get used so I don't believe it's the right way to go.  Toward the end, you'd have to be generating about 10,000 numbers before getting the only one left.  Plus you could leave yourself in an infinite loop if you didn't take into account the fact that all were used already.

If you wanted to use method #1 still, then you could build your array as mentioned before and then simply scan through your data grid and remove all the ones from that array that already inserted.  Then you have the advantage of being able to access a direct in-memory array, and you keep truly random numbers.  I believe this is the right way to go.

hope this helps.  have a good one


Author Comment

ID: 16857627
#1 sounds good to me. I'm not too bothered about the numbers being 'truly random' as long as

a) I don't get any duplicate anything already in the datagrid.
b) The user cannot guess what number will be next.

Our order numbers have to be 5 digits and range between 500000-600000. Looking at it realistically, the datagrid will never be populated with more than 1000 items (and that estimate is on the high side). This may speed up #2 a fair bit but #1 still seems the better option.

As my VB is not too hot ,something else I'm toying with (but it sounds a bit of a bodge) is to have the random numbers pre-generated into a text file then called up as required and then deleted to avoid duplicates. I've really not sold myself on this idea though!
LVL 20

Assisted Solution

clarkscott earned 1000 total points
ID: 16859895
Sounds like you need to file your results (only way to keep things straight if you turn off and turn back on again)

As you select each number, update a boolean field in the record. As each random number is selected, retreive the 'table' record and examine the boolean field - if checked, repeat the random process until a non-checked record is located.  If found - use the number and check the boolean field in the record.

To reset, simply update all boolean fields to false.

Question: How can 5 digit order numbers range between 500000 - 600000? (6 digit order numbers?)

Scott C

Author Comment

ID: 16871670
Thanks guys. I'll try your suggestions and see what I can come up with.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question