random
05-05-2010, 04:46 PM
Option Explicit
Public Function RL(Cnt1 As Integer, Cnt2 As Integer, MySet As Integer)
Dim Rand As String
Dim i As Integer, RndNo As Integer, XSet As Integer
Dim MyCase As Integer
Application.Volatile
Select Case MySet
Case Is = "1" 'Upper case
MyCase = 65: XSet = 26
Case Is = "2" 'Lower Case
MyCase = 97: XSet = 26
Case Is = "3" 'Leading Capital
MyCase = 97: XSet = 26
Case Is = "4" 'Text digits
MyCase = 48: XSet = 10
Case Is = "5" 'Numeric digits
MyCase = 48: XSet = 10
End Select
If MySet = 3 Then 'Set leading character of "Name"
i = i + 1
Randomize
Rand = Rand & Chr(Int((26) * Rnd + 65))
End If
'Set random length of string
RndNo = Int((Cnt2 + 1 - Cnt1) * Rnd + Cnt1)
Do
i = i + 1
Randomize
Rand = Rand & Chr(Int((XSet) * Rnd + MyCase))
Loop Until i = RndNo
RL = Rand
'Convert string to number
If MySet = 5 Then RL = RL * 1
End Function
Thank you for this, but if you wanted each character within the string to be unique within the context of the string, how could you rewrite this?
For example, I wouldn't want to generate a random string of 5 letters where 1 letter shows up more than once, e.g. ABCDD.
Thanks in advance!
Public Function RL(Cnt1 As Integer, Cnt2 As Integer, MySet As Integer)
Dim Rand As String
Dim i As Integer, RndNo As Integer, XSet As Integer
Dim MyCase As Integer
Application.Volatile
Select Case MySet
Case Is = "1" 'Upper case
MyCase = 65: XSet = 26
Case Is = "2" 'Lower Case
MyCase = 97: XSet = 26
Case Is = "3" 'Leading Capital
MyCase = 97: XSet = 26
Case Is = "4" 'Text digits
MyCase = 48: XSet = 10
Case Is = "5" 'Numeric digits
MyCase = 48: XSet = 10
End Select
If MySet = 3 Then 'Set leading character of "Name"
i = i + 1
Randomize
Rand = Rand & Chr(Int((26) * Rnd + 65))
End If
'Set random length of string
RndNo = Int((Cnt2 + 1 - Cnt1) * Rnd + Cnt1)
Do
i = i + 1
Randomize
Rand = Rand & Chr(Int((XSet) * Rnd + MyCase))
Loop Until i = RndNo
RL = Rand
'Convert string to number
If MySet = 5 Then RL = RL * 1
End Function
Thank you for this, but if you wanted each character within the string to be unique within the context of the string, how could you rewrite this?
For example, I wouldn't want to generate a random string of 5 letters where 1 letter shows up more than once, e.g. ABCDD.
Thanks in advance!