?
Solved

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

Posted on 2003-11-06
7
Medium Priority
?
325 Views
Last Modified: 2008-02-01
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
                  sql = "SELECT * FROM DownloadID"
                  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
                               ' was already used so
                                bGoodToGo = False
                                Response.Write "Number does exist<br>" & VBCRLF
                                Exit Do
                        End If
                 oRS.MoveNext
                 Loop
                End If    
              loop
        End If
End Function
0
Comment
Question by:mfwebteam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 

Author Comment

by:mfwebteam
ID: 9696846
The line sql = "insert into NewTravelRequest" &_
Should read : "insert into DownloadID" &_
0
 
LVL 58

Expert Comment

by:Gary
ID: 9697000
...and the problem is?
0
 
LVL 3

Expert Comment

by:Neil Thompson
ID: 9699805
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:Neil Thompson
ID: 9699806
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
0
 

Author Comment

by:mfwebteam
ID: 9701071
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.
0
 
LVL 3

Accepted Solution

by:
Neil Thompson earned 500 total points
ID: 9701207
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
   '... add to database
   '... 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
0
 

Author Comment

by:mfwebteam
ID: 9701360
I want to set the number of clicks on the url so that I can expire the link after a certain number.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question