Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random string not so random! 1

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I am working on a system that creates a logical file structure with folders stored in a database. I each folder has a unique identifier. My function to create this is with code I got from the Microsoft website:

Code:
Function CreateGUID()
  Randomize Timer
  Dim tmpCounter,tmpGUID
  Const strValid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  For tmpCounter = 1 To 20
    tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1)
  Next
  CreateGUID = tmpGUID
End Function

For some mysterious reason I am getting the occasional folders created with identical GUID values which I would have thought should be extremely unlikely. I also store files in the database with a GUID as well and these also get duplicated from time to time.

Any ideas as to why this might be happening? I thought Randomize Timer was meant to address this sort of issue...

Thanks very much

Ed
 
Hi Ed,

From the Microsoft website:
For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previously generated number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.

I suggest you move the Randomize statement inside the For loop. This way Rnd will return values independent of the previously generated value.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Or use:
Code:
Option Explicit

Dim TypeLib, GUID
Set TypeLib = CreateObject("Scriptlet.TypeLib")
GUID = TypeLib.Guid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top