# How to generate a random number (13 digits) between 9999999999999 - 0000000000001

Hello,
I am trying to generate a random number and I am sucessful doing that:
Randomize
strOrderNo = INT((9999999999999 - 0000000000001 + 1) * RND + 0000000000001)

Then update the database with this number plus other options for example depending on what I passed and evently when the click the link I would check the querystring to update the database for each click they do or tell them it is expired:

Function Randomize(Clicks,ExpireDate,ExpireTime)
Dim sql
If bGoodToGo = True Then
sql = "insert into NewTravelRequest" &_
"(ID,Number,Clicks,ExpireDate,ExpireTime)" &_
"values(" & ID & ", '" & Number & "', '" & Clicks & "', '" & ExpireDate & "', '" ExpireTime & "')"
Else
Do Until bGoodToGo
Randomize
strOrderNo = INT((9999999999999 - 0000000000001 + 1) * RND + 0000000000001)
'strOrderNo = "8888888888888"

bGoodToGo = False
If Len(strOrderNo) = 13 Then
' we have a 13 char string so now test DB that this 13 char Key is unique
Response.Write "We have 13 chars<br>" & VBCRLF
' TODO: Do DB test here
oRS.Open sql, oConn, 3, 3
Do until oRS.EOF
strDBNo = Trim(oRS("Number"))
Response.Write "Database Numbers: " & strDBNo & "<br>" & VBCRLF
If strDBNo <> strOrderNo Then
bGoodToGo = True
Response.Write "Number doesn't exist<br>" & VBCRLF
Else
bGoodToGo = False
Response.Write "Number does exist<br>" & VBCRLF
Exit Do
End If
oRS.MoveNext
Loop
End If
loop
End If
End Function
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
The line sql = "insert into NewTravelRequest" &_
Commented:
...and the problem is?
Senior Systems DeveloperCommented:
The easiest way for a truly random mix (including upper/lower characters) how about:

<% Option Explicit %>

<%
Response.Write RandomPW(13) & "<br>" & vbCrLf

Function RandomPW(myLength)

Dim X, Y, strPW

If myLength = 0 Then
Randomize
myLength = Int((10 * Rnd) + 10)
End If

For X = 1 To myLength
'Randomize the type of this character
Y = Int((3 * Rnd) + 1)

Select Case Y
Case 1'Numeric character
Randomize
strPW = strPW & CHR(Int((9 * Rnd) + 48))
Case 2
Randomize
strPW = strPW & CHR(Int((25 * Rnd) + 65))
Case 3
Randomize
strPW = strPW & CHR(Int((25 * Rnd) + 97))

End Select
Next

RandomPW = strPW

End Function

%>

Neil
Senior Systems DeveloperCommented:
The easiest way for a truly random mix (including upper/lower characters) how about:

<% Option Explicit %>

<%
Response.Write RandomPW(13) & "<br>" & vbCrLf

Function RandomPW(myLength)

Dim X, Y, strPW

If myLength = 0 Then
Randomize
myLength = Int((10 * Rnd) + 10)
End If

For X = 1 To myLength
'Randomize the type of this character
Y = Int((3 * Rnd) + 1)

Select Case Y
Case 1'Numeric character
Randomize
strPW = strPW & CHR(Int((9 * Rnd) + 48))
Case 2
Randomize
strPW = strPW & CHR(Int((25 * Rnd) + 65))
Case 3
Randomize
strPW = strPW & CHR(Int((25 * Rnd) + 97))

End Select
Next

RandomPW = strPW

End Function

%>

Neil
Author Commented:
The problem is that I am not sure how to do it in the following steps.
1. Generate the random number
2. Check if it is in the databse
- If not: then insert into database
- If yes: then run the random generator again
3. have a parameter like "click" that I can specify so that everytime the user clicks on the link it will update the database in the clicks column with 1 then 2 then 3 then 4 and so on until it hits the number I specified.

Hopes this clears up what I want to do.
Senior Systems DeveloperCommented:
1) if you use my code above simply:

Open the db connection

'Set a variable
varRandNum = RandomPW(13)

...select * from DB WHERE [YOUR NUMBER FIELD] = '" & varRandNum & "'"

'check if it is EOF :

if oRS.eof ' number does not exist
'... update
else
' call the random generator again
varRandNum = RandomPW(13)
' recheck with another loop if required
' update the database
end if

' close oRS and db

why do you want to update the database with clicks 1 ++ ?

Neil

Experts Exchange Solution brought to you by