1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Generate a Random Unique 4 digit number

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

  1. GreenMtns

    GreenMtns New Member

    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_F

    E_E_F New Member

    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


    "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.
  3. mugnebot

    mugnebot New Member

    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.
    oh wait isnt this a bash forum? oh well.
    E_E_F likes this.
  4. E_E_F

    E_E_F New Member


    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 pillai

    apr pillai New Member

    Try this 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
    End Function
    shabbir likes this.

Share This Page