- **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/*)

Generate a Random Unique 4 digit numberHi
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 |

Re: Generate a Random Unique 4 digit numberI 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. |

Re: Generate a Random Unique 4 digit numberhere 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. |

Re: Generate a Random Unique 4 digit numberGreenMtns,
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. |

Re: Generate a Random Unique 4 digit numberTry this Code:
Code:
`Public Function RandomNumbers()` |

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