serial number generator program
hello,
I need some help pls: i want to create a program that will generate a serial number composed of 8 carattercts (3 letters followed by 5 numbers ---> i.e. FRT78136). Unfortunately i don't know how to use visualbasic very well; the last program i learnt was gwbasic...using gwbasic i would have used the randomize function. The program should change the rnd function in accordance to a sspecific yesr (i.e. when i am prompted for the 'randomize seed number' this should be a specific year such as 2010).
thank you for your kind help
Debora
Re: serial number generator program
Search the Code Bank section on the forum for "serial generator", and check out Planet Source Code.
Re: serial number generator program
Real quick sample
Code:
Option Explicit
Private Sub Command1_Click()
MsgBox GenerateSerialNumber
End Sub
Private Function GenerateSerialNumber() As String
Dim i As Integer
Dim strChar(7) As String
Randomize
For i = 0 To 7
If i < 3 Then
strChar(i) = Chr(Int(Rnd() * 26) + 65)
Else
strChar(i) = Int(Rnd() * 10)
End If
Next i
GenerateSerialNumber = Join(strChar, "")
End Function
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
Re: serial number generator program
Quote:
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.
Re: serial number generator program
Quote:
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?
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
Re: serial number generator program
So, what is the difference between :
Format$((.Data4_4To7 And &H7FFFFFFF) Mod 100000, "00000")
and
Format$(Int(Rnd() * 100000), "00000")
I've tried both and the mean of the results of thousands of repetitions looks to be the same --> 50,000.
Re: serial number generator program
No extra variable or loop.
Code:
Option Explicit
Private Sub Form_Load()
Randomize
End Sub
Private Sub cmdGetSN_Click()
txtSN.Text = RndSN()
End Sub
Function RndSN() As String
'-- no guaranty SN is unique
RndSN = Chr$(Int(Rnd * 26) + 65) & Chr$(Int(Rnd * 26) + 65) & _
Chr$(Int(Rnd * 26) + 65) & Format(Int(Rnd * 100000), "00000")
End Function
Re: serial number generator program
Quote:
Originally Posted by
anhn
No extra variable or loop.
Code:
Option Explicit
Private Sub Form_Load()
Randomize
End Sub
Private Sub cmdGetSN_Click()
txtSN.Text = RndSN()
End Sub
Function RndSN() As String
'-- no guaranty SN is unique
RndSN = Chr$(Int(Rnd * 26) + 65) & Chr$(Int(Rnd * 26) + 65) & _
Chr$(Int(Rnd * 26) + 65) & Format(Int(Rnd * 100000), "00000")
End Function
I considered that, but I figured everybody would yell at me for "slow" string concatenation. I thought MarkT's Join() of the simple array might be faster. Maybe Merri can test them.
Good one, AnHn. :thumb:
Re: serial number generator program
Why would speed even be an issue here? Nobody is going to need thousands of these at once.
I completely missed the "year" requirement. I'm not sure where that comes in.
Re: serial number generator program
Speed is not an issue here but for CodeDoc:
My function RndSN() is much simpler and about 30% faster than your function GenerateSerialNumber() in Post#6.
Edit: If you want a faster function (but longer code) then this one is 3 times faster:
Code:
Function RndSN2() As String
'-- no guaranty SN is unique
Dim n As Long
RndSN2 = "00000000"
n = Int(Rnd * 100000)
Mid$(RndSN2, 8 - Len(n)) = n
Mid$(RndSN2, 1, 1) = Chr$(65 + Int(Rnd * 26))
Mid$(RndSN2, 2, 1) = Chr$(65 + Int(Rnd * 26))
Mid$(RndSN2, 3, 1) = Chr$(65 + Int(Rnd * 26))
End Function
Re: serial number generator program
Quote:
Originally Posted by
anhn
Speed is not an issue here but for CodeDoc:
My function RndSN() is much simpler and about 30% faster than your function GenerateSerialNumber() in Post#6.
Edit: If you want a faster function (but longer code) then this one is 3 times faster:
Code:
Function RndSN2() As String
'-- no guaranty SN is unique
Dim n As Long
RndSN2 = "00000000"
n = Int(Rnd * 100000)
Mid$(RndSN2, 8 - Len(n)) = n
Mid$(RndSN2, 1, 1) = Chr$(65 + Int(Rnd * 26))
Mid$(RndSN2, 2, 1) = Chr$(65 + Int(Rnd * 26))
Mid$(RndSN2, 3, 1) = Chr$(65 + Int(Rnd * 26))
End Function
Truly exceptional. Mid$() replace blows both simple string concatenation and Join() away. Not sure exactly why but it does. :thumb:
Dilettante said, "I completely missed the "year" requirement. I'm not sure where that comes in."
--------------
Neither do I. I'm not sure what that means to the problem. Perhaps it has something to do wth seeding Randomize, but the whole concept seemed nebulous. So, I did not address it either.
Re: serial number generator program
Maybe a new repeating series each year?
Re: serial number generator program
To me it just looks like the OP was using the year as the seed for the Rnd or Randomize seed.