Tuesday, October 2, 2007

[vb6] Get the Random Number

Following function to return the random value in string for certain length.
Private Function GetRandomVal(ByVal pLength%, Optional ByVal pSequence&) As String
If pSequence = 0 Then Randomize Else Randomize pSequence
GetRandomVal = _
Right(String(pLength - 1, "0") & _
Format(CSng(((10 ^ pLength) - 1 + 1) * Rnd + 1) + pSequence, _
String(pLength, "#")), pLength)
End Function

Private Sub Form_Load()
MsgBox GetRandomVal(4) & vbNewLine & _
GetRandomVal(5, 1) & vbNewLine & _
GetRandomVal(5, 2) & vbNewLine
End Sub

No comments:

Post a Comment