Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MS Access (http://www.go4expert.com/forums/ms-access-forum/)
-   -   Generate a Random Unique 4 digit number (http://www.go4expert.com/forums/generate-random-unique-4-digit-t12631/)

GreenMtns 1Aug2008 22:05

Generate a Random Unique 4 digit number
 
Hi
I am new to this board and am trying to work out a little problem. I have a table of drivers who all have an Primary Key of ID numbers auto generated as each new record is saved. Each driver also has a unique 4 digit number that they use when logging into each vehicle. This has to be a random number to prevent drivers from knowing anyone else's number. As each new driver is added to the record the Random unique number should be automatically generated. I have been through my MS Access VB for Applications Fundamentals book many times and cannot find a solution to this. I would appreciate any suggestions on this.

Thank You

E_E_F 3Aug2008 16:20

Re: Generate a Random Unique 4 digit number
 
I think a "Truly" random number would not be your best choice in this situation. You would probably be wise to use a algorithm like:

(prime_number1 ^ record_number ) modulus prime_number2

Where:

"prime_number1 ^ record_number" means prime_number1 to power of record_number

and where:

prime_number1 and prime_number2 should be around 10000 and relative prime.

Mind that i.e. (a ^ 10) mod b == ( ( (a ^ 9) mod b ) * a ) mod b
This way you can create a simple program loop to calculate the power / mod of a larger number.

This way all values will be unique, but you have to keep the prime_number1 & 2 secret and in a safe place.

Don't ask me for an algorithm to find primes please.

For more information you read some more about RSA algorithm.

mugnebot 17Nov2010 23:38

Re: Generate a Random Unique 4 digit number
 
here is a way to get a file name that is really random, it uses the epoch date plus 4 random digits.
randfile="r$(date +'%s')$(($RANDOM%10))$(($RANDOM%10))$(($RANDOM%10) )$(($RANDOM%10))";
or just the four random digits.
FourRandDigits="$(($RANDOM%10))$(($RANDOM%10))$(($ RANDOM%10))$(($RANDOM%10))";
oh wait isnt this a bash forum? oh well.

E_E_F 18Nov2010 14:19

Re: Generate a Random Unique 4 digit number
 
GreenMtns,

Combining the date and a random number is a solution, but there is a (very small) chance that a duplicate PIN / ID is generated when two or more ID 's are generated on the same day, so you should check if the ID already exists if you want to be 100% safe.

If the random were just a PIN, to be combined with additional ID it would not be a problem. Duplicate PIN is common in debit cards, but they must always be combined with a unique ID.

Furthermore, this solution exeeds the 'four digit criterium'.

Otherwise it's a simple and good solution, thanks for contributing to the community.

apr pillai 17Dec2010 22:49

Re: Generate a Random Unique 4 digit number
 
Try this Code:
Code:

Public Function RandomNumbers()
Dim rn As Long, j, k
Randomize (Timer)
For j = 1 To 50
  k = Int(Rnd(1) * (9999 - 1000)) + 1000
  Debug.Print k
Next


End Function



All times are GMT +5.5. The time now is 08:18.