Random number with a twist....

Posted on 2006-06-07
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
    LVL 7

    Expert Comment

    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.

    LVL 7

    Expert Comment

    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

    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????
    LVL 7

    Accepted Solution

    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

    #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

    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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    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…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now