How random are randomly generated numbers ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi there. Going straight to the problem. (Access 2000, Jet SP8, WinXP)

I have a table that has a string primary key, consisting of 25 numbers,
which are generated randomly using the following function:


Public Function NUMRandom(lowerbound As Long, upperbound As Long) As Long
NUMRandom = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Function

The function above is called from the following function:


Public Function APPCreateCode() As String
Dim s As String
Dim i As Integer

s = ""

For i = 1 To 25
Randomize
s = s & CStr(NUMRandom(0, 9))
Next

APPCreateCode= s
End Function

After a couple of weeks, this code started producing duplicate key values (I
don't know why, but I got an Access message when trying to insert.) After
this point, whenever the user pressed a button to insert a record, he always
got the duplicate key message. He had to click over twenty times, to get the
record inserted with a random primary key (and sometimes he' d give up
trying). So I changed the code a little as follows:


Public Function APPCreateCode() As String
Dim s As String
Dim i As Integer, j As Integer, li_rnd As Integer
Dim ls_temp As String

s = ""

Randomize
li_rnd = NUMRandom(1, 30)

For i = 1 To 25
For j = 1 To li_rnd
Randomize
ls_temp = CStr(NUMRandom(0, 9))
Next

s = s & ls_temp
Next

APPCreateCode= s
End Function
--------------
What I did, was to execute the randomize function a few more times (and also
for a random number of times in the inside loop. Then I gave the application
back to the client, and everything went well, but only for a couple of
weeks. Then the same problem occured again. Still the user got the duplicate
key message. This is an unexpected access behavior to me and I don't know
what I do wrong.


QUESTIONS:

1) How many characters are compared in the primary by Access, before
displaying the duplicate key message? (a) All of them in the key, or (b)
only a few?

2) If (a), then is there any better way of producing this primary key?

3) Is something wrong with the randomize and rnd functions?

TIA
Savvoulidis Iordanis
Greece



.



Relevant Pages

  • Trouble using ADOX to create linked tables in jet database from an
    ... The tables in the ODBC data source does not have a primary key. ... As String, ByVal strSourceTbl As String, ByVal strLinkTblName As String) ... Dim catDB As ADOX.Catalog ... Dim tblLink As ADOX._Table ...
    (microsoft.public.data.ado)
  • Re: Problem with insert statement...
    ... The table has a primary key and under normal circumstances I can edit the ... The connection string is ... lngMemberID As Long, ByVal strReport As String) As Boolean ... Dim adp As OleDb.OleDbDataAdapter ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Count function in Query
    ... yes, RequiredDate is not a primary key, however, a composite primary key ... Dim strWhere As String 'String to use as WhereCondition ...
    (microsoft.public.access.queries)
  • RE: Adding new records based on current record
    ... Public Function CopyRecord(strTable As String, ... Dim cmd As ADODB.Command ... Dim strRecordType as String ... the name of its primary key column and the value of the newly ...
    (microsoft.public.access.gettingstarted)
  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)