# Generate a Random Unique 4 digit number

Discussion in 'MS Access' started by GreenMtns, Aug 1, 2008.

1. ### GreenMtnsNew Member

Joined:
Jul 25, 2008
Messages:
1
0
Trophy Points:
0
Occupation:
Tour Coach Co. Safety Director
Location:
Vermont
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

2. ### E_E_FNew Member

Joined:
Aug 3, 2008
Messages:
6
0
Trophy Points:
0
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.

3. ### mugnebotNew Member

Joined:
Feb 16, 2009
Messages:
13
1
Trophy Points:
3
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 likes this.
4. ### E_E_FNew Member

Joined:
Aug 3, 2008
Messages:
6
0
Trophy Points:
0
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.

5. ### apr pillaiNew Member

Joined:
Dec 17, 2010
Messages:
16
12
Trophy Points:
0
Occupation:
Auditor
Location:
India
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

```

shabbir likes this.