J
Joseph Atie
Ive got a piece of code that is supposed to generate a random 9 digit number.
the problem comes when i take the output from this function and use it to
generate some sql to insert records to a mysql db
the 9 digit code is supposed to be my primary key in the mysql db.
unfortunate im getting duplications on my primary key.
ive tried to increase the how random the number is by mutlipling 3
iterations of the rnd fuction (it is randomize each time)
but no matter what i try to make the number more random i still end up with
say 5 duplications over 1000 records. at the duplications
here is the code im using
Public Function RandomNumbers(Lowest As Long, Highest As Long, _
Optional Decimals As Integer)
If IsMissing(Decimals) Or Decimals = 0 Then
Randomize
factor = Rnd
Randomize
factor = factor * Rnd
Randomize
factor = factor * Rnd
RandomNumbers = Int((Highest + 1 - Lowest) * factor + Lowest)
Else
Randomize
factor = Rnd
Randomize
factor = factor * Rnd
Randomize
factor = factor * Rnd
RandomNumbers = Round((Highest - Lowest) * factor + Lowest, Decimals)
Randomize
RandomNumbers = RandomNumbers * Rnd + Lowest
End If
End Function
Any thoughts?
the problem comes when i take the output from this function and use it to
generate some sql to insert records to a mysql db
the 9 digit code is supposed to be my primary key in the mysql db.
unfortunate im getting duplications on my primary key.
ive tried to increase the how random the number is by mutlipling 3
iterations of the rnd fuction (it is randomize each time)
but no matter what i try to make the number more random i still end up with
say 5 duplications over 1000 records. at the duplications
here is the code im using
Public Function RandomNumbers(Lowest As Long, Highest As Long, _
Optional Decimals As Integer)
If IsMissing(Decimals) Or Decimals = 0 Then
Randomize
factor = Rnd
Randomize
factor = factor * Rnd
Randomize
factor = factor * Rnd
RandomNumbers = Int((Highest + 1 - Lowest) * factor + Lowest)
Else
Randomize
factor = Rnd
Randomize
factor = factor * Rnd
Randomize
factor = factor * Rnd
RandomNumbers = Round((Highest - Lowest) * factor + Lowest, Decimals)
Randomize
RandomNumbers = RandomNumbers * Rnd + Lowest
End If
End Function
Any thoughts?