INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Random string not so random!

Random string not so random!

(OP)
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

RE: Random string not so random!

Hi Ed,

From the Microsoft website:

Quote:

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.

RE: Random string not so random!

Or use:

CODE -->

Option Explicit

Dim TypeLib, GUID
Set TypeLib = CreateObject("Scriptlet.TypeLib")
GUID = TypeLib.Guid 

RE: Random string not so random!

(OP)
Thanks guitarzan - really helpful!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close