|
-
Jul 15th, 2010, 11:55 AM
#1
Re: serial number generator program
Not sure if this improves the speed of MarkT's code, but it does seem to knock out 4 looping iterations:
Code:
Option Explicit
Private Sub Command1_Click()
MsgBox GenerateSerialNumber
End Sub
Private Function GenerateSerialNumber() As String
Dim strChar(3) As String, i As Integer
Randomize
For i = 0 To 3
If i < 3 Then
strChar(i) = Chr(Int(Rnd() * 26) + 65)
Else
strChar(i) = Format$(Int(Rnd() * 100000), "00000")
End If
Next i
GenerateSerialNumber = Join(strChar, "")
End Function
-
Jul 15th, 2010, 12:20 PM
#2
Re: serial number generator program
 Originally Posted by Code Doc
Not sure if this improves the speed of MarkT's code, but it does seem to knock out 4 looping iterations:
I thought about doing it that way but decided to do a random digit as each spot. If you just do one random number for the last five digit you will end up with a lot of low numbers that will have a number of zeros padding them.
-
Jul 15th, 2010, 02:24 PM
#3
Re: serial number generator program
 Originally Posted by MarkT
I thought about doing it that way but decided to do a random digit as each spot. If you just do one random number for the last five digits you will end up with a lot of low numbers that will have a number of zeros padding them.
Hmmm... I generated 100,000 random 5-digit numbers this way several times and the average is always very near 50,000 and sometimes over 50,000. To me, that indicates no small number bias whatsoever. Give it a try:
Code:
Private Sub Command1_Click()
Dim Sum As Single, Num As Long
Num = 100000
For i = 1 To Num
Call GenerateSerialNumber
Sum = Sum + CSng(Right$(GenerateSerialNumber, 5))
Next
MsgBox Format$(Sum / Num, "##,###.##")
End Sub
Private Function GenerateSerialNumber() As String
Dim strChar(3) As String
For i = 0 To 3
If i < 3 Then
strChar(i) = Chr(Int(Rnd() * 26) + 65)
Else
strChar(i) = Format$(Int(Rnd() * 100000), "00000")
End If
Next i
GenerateSerialNumber = Join(strChar, "")
End Function
Private Sub Form_Load()
Randomize
End Sub
If a distribution of random 5-digit numbers were biased toward small numbers (and thus not random), would not the mean of the distribution have to be significantly less than the theoretical mean of 50,000?
Last edited by Code Doc; Jul 15th, 2010 at 02:53 PM.
Doctor Ed
-
Jul 15th, 2010, 06:18 PM
#4
Re: serial number generator program
Well I'd probably use something more like:
Code:
Option Explicit
Private Type GUID
Data1_Lo As Integer
Data1_Hi As Integer
Data2 As Integer
Data3 As Integer
Data4_0To3 As Long
Data4_4To7 As Long
End Type
Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid As GUID) As Long
Private Function GenSerial() As String
Dim udtGUID As GUID
Dim intA As Integer
If CoCreateGuid(udtGUID) Then
GenSerial = "*error*"
Else
intA = Asc("A")
With udtGUID
GenSerial = _
Chr$(intA + (.Data1_Lo And &H7FFF) Mod 26) _
& Chr$(intA + (.Data2 And &H7FFF) Mod 26) _
& Chr$(intA + (.Data3 And &H7FFF) Mod 26) _
& Format$((.Data4_4To7 And &H7FFFFFFF) Mod 100000, "00000")
End With
End If
End Function
Private Sub cmdGenerate_Click()
txtSerial.Text = GenSerial()
End Sub
Last edited by dilettante; Jul 15th, 2010 at 06:30 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|