wildarmsdave
asked on
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.
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.
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.
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.
Good luck.
ASKER
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????
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
#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!
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!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys. I'll try your suggestions and see what I can come up with.