Random 11 digit alphanumeric number?

G

Guest

Can anyone tell me how I can generate a random 11-digit alphanumeric number?

Much thanks,
Clint
 
J

John Spencer

Pardon me but I think that routine returns prime numbers.

Try the following.

Public Function RandomNumber(dblMin As Double, dblMax As Double) As Double
'Returns a random number between the dblMin and dblMax
'If you want a whole number then use CLng on the result
Static tfRandom As Boolean

If tfRandom = False Then
Randomize Timer
tfRandom = True
End If

RandomNumber = Rnd * (dblMax - dblMin) + dblMin

End Function

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
G

Guest

Thanks for the code ref, but I need to come up with an alphanumeric string,
nut just a numeric string. If you have any refs for something like this, I
would appreciate it if you could pass on!

Thanks,
Clint
 
J

John Spencer

Ok, can you be a bit more specific on what you want? A random alpha
numeric string can be generated by using a random number generator to
generate a number 8 times that is between x and y and then using that in
combination with Chr(n) to return a number or letter.

Do you have any rules on generating the 8-character string? Here are
some possible rules - some of them conflict with other
-- Only numbers and Letters
-- All upper case
-- All lower case
-- No spaces
-- at least one number
-- at least one letter
-- Must start with a letter
-- must start with a number
-- every character must be unique
-- no adjacent characters can be alike
-- any 8 characters from Asc(48) to Asc(125)

The code for the last would be something like the following using the
RandomNumber function I posted earlier.

For I = 1 to 8
YourString = YourString & Chr(CLng(RandomNumber(48,125)))
Next I

'====================================================
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================
 
R

raskew via AccessMonster.com

Hi -
The following will produce random alpha-numeric numbers based on a pattern
the user provides:

Function runrandom(pvar As Variant) As String
'*******************************************
'Purpose: Driver to produce random letter/
' number combinations using the
' input string as a template
'Coded by: raskew
'Calls: Function Randomnum()
'Inputs: ? runrandom("A1AA9")
'Output: X9DY5
'*******************************************

Dim i As Integer
Dim strvalue As String
For i = 1 To Len(pvar)
strvalue = strvalue & Randomnum(IIf(Asc(Mid(pvar, i, 1)) < 65, True,
False))
Next i
runrandom = strvalue
End Function
Function Randomnum(ptype As Boolean) As String
'*******************************************
'Name: Randomnum (Function)
'Purpose: Produce random number (0 to 9) if
' ptype is True, random letter (A to Z)
' if ptype is False
' Note: Based on code published by
' Robert L. Johnson III at
'
http://www.tek-tips.com/viewthread.cfm?SQID=433696&SPID=702&page=3
'*******************************************
Dim intNumber As Integer
Randomize
intNumber = Int((IIf(ptype, (9 - 0 + 1), (26 - 1 + 1))) * Rnd + IIf(ptype, 0,
1))
Randomnum = IIf(ptype, CStr(intNumber), Chr(intNumber + 64))
End Function

HTH - Bob

John said:
Ok, can you be a bit more specific on what you want? A random alpha
numeric string can be generated by using a random number generator to
generate a number 8 times that is between x and y and then using that in
combination with Chr(n) to return a number or letter.

Do you have any rules on generating the 8-character string? Here are
some possible rules - some of them conflict with other
-- Only numbers and Letters
-- All upper case
-- All lower case
-- No spaces
-- at least one number
-- at least one letter
-- Must start with a letter
-- must start with a number
-- every character must be unique
-- no adjacent characters can be alike
-- any 8 characters from Asc(48) to Asc(125)

The code for the last would be something like the following using the
RandomNumber function I posted earlier.

For I = 1 to 8
YourString = YourString & Chr(CLng(RandomNumber(48,125)))
Next I

'====================================================
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================
Thanks for the code ref, but I need to come up with an alphanumeric string,
nut just a numeric string. If you have any refs for something like this, I
[quoted text clipped - 12 lines]
 
R

RD

Hi there,

I don't know if you've found a satisfactory solution yet.

Here is one I built some time back. It's tested and works fine.

Function makestr() As String
Dim str As String
Dim ChooseNumber As Integer
Dim Myvalue As Integer, i As Integer

str = ""
Do While Len(str) < 5
Randomize 'Initialize random number generator
ChooseNumber = Int((3 * Rnd) + 1) 'Using 2 makes 50%, 3 favors letters
If ChooseNumber = 1 Then 'Make it a number
Myvalue = Int((9 * Rnd) + 1)

'add number
str = str & Myvalue
Else 'Make it a letter

'Add letter
Myvalue = Int((26 * Rnd) + 1)
str = str + Chr(64 + Myvalue)
End If
Loop
makestr = str
Debug.Print makestr
End Function

HTH,
RD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top