Link to home
Start Free TrialLog in
Avatar of SethCrews_1
SethCrews_1

asked on

Create Random Unique Numbers in VB.NET

I need to generate random numbers that fit the following crieria:

1) The value must be random.
2) The value must not repeat any previous number (it must be Unique).
3) I need to generate this numeric only value in VB.NET

Randomize will get me a pseudo-random number but it will repeat previous values.  I could make my sample range very large to make the probability of a collision very low, but there would still be a chance of a repeated value.  I don't want to just use a counter because the value would be predictable.

Any thoughts or suggestions are welcome !
Avatar of javajws
javajws
Flag of United States of America image

Avatar of Shakti109
Shakti109


A few questions :
1) what is the depth of your uniqueness? In other words, eventually everything will repeat for a given depth. If you specify a number that is 3 digits long then you will end up with repition after 999.

2) What is the maximum length of the number you want to use?

Thought process :
If you are trying to generate a random number, within a defined range (lets say 3 digits), then :
1) create a "random" function
2) Check the number generated against previous number
3) If the number matches, re-run the function
4) If the number does NOT match, display the number and record it
5) keep going until you exhaust the values.

To generate the number you could use the current time as the "seed" value. If the machine is very fast and you are trying to speed through the given depth then you would have to check to see if the seed had changed, if not wait a random amount then re-run.

You could further "complicate" this by alternating between adding/subtracting/multiplying/dividing random time intervals from the seed value.
It sounds to me like you are trying to accomplish the following:

1). Generate a gauranteed unique value
2). Have the unique value be completely unpredictable so that deciphering would be near impossible

Additionally you would like it to be a numeric value, that's where the trouble comes in.  I suggest spending a little time on elminating the dependency on the number if possible and switch to a GUID.
Avatar of SethCrews_1

ASKER

Shakti109,

1) The depth is 6 digits.
2) Max length is also 6.

I did look at seeding based on time, and I ran into the exact issue you mention where the seed value (time) caused a number to be repeated.  Based on the article from Javajws i'm wondering if that would solve that issue.  As for waiting a random amount and then re-running, i don't want to give up cycles because my seed isn't changing fast enough, because as CPU's get faster i'll lose more and more chances to get it done since time is constant :)

JustinVogt - you are correct.  I want a numeric only GUID.  I'm hoping I don't have to go away from numeric only as that actually would be a rather large undertaking.
ASKER CERTIFIED SOLUTION
Avatar of Shakti109
Shakti109

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Shakti109,

I did try JavaJWS' suggestion and as it described, separating the seeding from the generation does prevent back to back duplication of "random" numbers when multiple requests are made in short succession.

In my test program I found that with a depth of 6 and a max len of 6 that the first 1000 random numbers generated only 1 collision.  The issue is that the longer it is run, the higher the probability that a duplicate number will exist.  By the time the process had run 10,000 times i had 54 duplicated numbers so obviously over time the probability factor increases at an exponential rate.

Thank you for your suggestions.

Your observations are not uncommon in high-speed "random" generations :)

If you keep going down that path of observing random numbers being generated (and in later stages by very complex "random" functions) you may see some interesting results. By interesting, I mean that it almost becomes difficult to create randomess for a given depth, which itself is a very interesting observation...But thats a much deeper topic :)