[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

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

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
hannes1
Asked:
hannes1
1 Solution
 
Ryan ChongCommented:
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
 
pritam_dewanCommented:
i dont think u can really rely on generating unique random numbers.well u can try generating GUIDs.that will be unique globally.
0
 
Ryan ChongCommented:
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
 
glass_cookieCommented:
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
 
hannes1Author Commented:
It works fine for me.

Thanks

Hannes
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now