• Status: Solved
• Priority: Medium
• Security: Public
• Views: 219
• Last Modified:

# Random number with a twist....

Hi,

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?

Thanks,

Dave.
0
wildarmsdave
Asked:
• 3
• 3
2 Solutions

Commented:
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.

0

Commented:
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.

0

Author Commented:
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????
0

Commented:
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

0

Author Commented:
#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!
0

Commented:
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
0

Author Commented:
Thanks guys. I'll try your suggestions and see what I can come up with.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 3
• 3
Tackle projects and never again get stuck behind a technical roadblock.