Solved

Creating a random (non-duplicated) user id number.

Posted on 2002-03-14
5
169 Views
Last Modified: 2010-05-02
Is there a way whereby I can create a random ID number(of 5 or plus digits - numbers or letters) in VB. As long none of them are the same.

Is there an algorithm or function I can use? I don't know if Rnd can do the trick.

Thanks

Hannes


0
Comment
Question by:hannes1
5 Comments
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 200 total points
Comment Utility
Hi,

Try this:

Dim myArray() As Long
Dim i As Long, j As Long

Private Sub Command1_Click()
    GetRandomID 100
End Sub

Private Sub GetRandomID(ByVal iRange As Long)
    Erase myArray
    Randomize
    For i = 0 To iRange - 1
        j = Format$(Int(Rnd * 100000), "00000")
        Debug.Print j
    Next i
End Sub
0
 

Expert Comment

by:pritam_dewan
Comment Utility
i dont think u can really rely on generating unique random numbers.well u can try generating GUIDs.that will be unique globally.
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
sorry just not see carefully about "non-duplicated".

See this:
http://www.vbcode.com/asp/showsn.asp?theID=407

http://www.vbcode.com/asp/showsn.asp?theID=173
0
 
LVL 8

Expert Comment

by:glass_cookie
Comment Utility
Hi!

This is the code that I gave some time ago.  Chances are, it shouldn't be duplicated:

Dim myNumber As String, temp As Integer

For i = 0 To 9

Randomize
temp = Rnd * 10
If temp \ 2 = temp / 2 Then
myNumber = myNumber & Chr(48 + Int(Rnd * 10))
Else

Randomize
temp = Rnd * 10
If temp / 2 = temp \ 2 Then
myNumber = myNumber & Chr(65 + Rnd * 26)
Else
myNumber = myNumber & LCase(Chr(65 + Rnd * 26))
End If
End If

Next i

MsgBox myNumber


The thing is, using Randomize would make it more random.  

That's it!

glass cookie : )
0
 

Author Comment

by:hannes1
Comment Utility
It works fine for me.

Thanks

Hannes
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now