home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / random_number.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  4.1 KB  |  109 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. Dim intLowerBound    ' Lower bound of the random number range
  15. Dim intUpperBound    ' Upper bound of the random number range
  16.  
  17. Dim intRangeSize     ' Size of the range
  18. Dim sngRandomValue   ' A random value from 0 to intRangeSize
  19. Dim intRandomInteger ' Our final result - random integer to return
  20.  
  21.  
  22. ' Retrieve lower and upper bound requests if they're there
  23. ' o/w set to defaults of 0 and 100
  24. If IsNumeric(Request.QueryString("lowerbound")) Then
  25.     intLowerBound = CLng(Request.QueryString("lowerbound"))
  26. Else
  27.     intLowerBound = 0
  28. End If
  29.  
  30. If IsNumeric(Request.QueryString("upperbound")) Then
  31.     intUpperBound = CLng(Request.QueryString("upperbound"))
  32.     
  33.     ' Add a line to deal with default case of 0 to 0.
  34.     ' This really isn't neccessary, but I do it so the
  35.     ' sample doesn't default to generating a number between
  36.     ' 0 and 0 and always return 0 when no bounds are provided.
  37.     If intLowerBound = 0 And intUpperBound = 0 Then intUpperBound = 100
  38. Else
  39.     intUpperBound = 100
  40. End If
  41.  
  42.  
  43. ' Check for people asking for a number from in an inappropriate
  44. ' range (ie: 50 to 10) and swap the bounds
  45. If intLowerBound > intUpperBound Then
  46.     ' I really should've declared a temporary variable for this
  47.     ' swapping, but I was lazy and this one was already defined
  48.     ' and I don't use it till later... oh all right I'll do it
  49.     ' the "right" way... actually even this is bad... I should've
  50.     ' defined this up top... so sue me... hey it's free code what
  51.     ' do you want from me?
  52.     Dim iTemp
  53.     iTemp = intLowerBound
  54.     intLowerBound = intUpperBound
  55.     intUpperBound = iTemp
  56. End If
  57.  
  58.  
  59. ' Initialize the random number generator.
  60. ' Randomize can actually take parameters telling it how to initialize
  61. ' things, but for the most you'll just want to call it without passing
  62. ' it anything.
  63. Randomize()
  64.  
  65. ' Generate our random number.
  66. ' The Rnd function does most of the work.  It returns a value in the
  67. ' range 0 <= value < 1 so to generate a random integer in the specified
  68. ' range we need to do some calculation.  Specifically we take the size
  69. ' of the range in which we want to generate the number (add 1 so the
  70. ' upper bound can be generated!) and then multiply it by our random
  71. ' element.  Then to place the value into the correct range of numbers
  72. ' we add the lower bound.  Finally we truncate the number leaving us
  73. ' with the integer portion which is always somewhere between the
  74. ' lower bound and upper bound (inclusively).
  75.  
  76. ' Find range size
  77. intRangeSize = intUpperBound - intLowerBound + 1
  78.  
  79. ' Get a random number from 0 to the size of the range
  80. sngRandomValue = intRangeSize * Rnd()
  81.  
  82. ' Center the range of possible random numbers over the desired result set
  83. sngRandomValue = sngRandomValue + intLowerBound
  84.  
  85. ' Convert our value to an integer
  86. intRandomInteger = Int(sngRandomValue)
  87.  
  88.  
  89. ' The above 4 lines are equivilent to the popular shorter version
  90. ' below.  I split it up so I could indicate what each step is doing.
  91. ' intRandomInteger = Int((intUpperBound - intLowerBound + 1) * Rnd + intLowerBound)
  92.  
  93. ' Show out output indicating what we've done and our result.
  94. %>
  95. You asked for a random number between <B><%= intLowerBound %></B> and <B><%= intUpperBound %></B>.<BR>
  96. The computer returned: <B><%= intRandomInteger %></B><BR>
  97.  
  98. <!-- Build the form for user input -->
  99. <FORM ACTION="random_number.asp" METHOD="get" NAME="frmRandomNumberBounds">
  100.  
  101. Generate a random number between
  102. <INPUT TYPE="text" NAME="lowerbound" VALUE="<%= intLowerBound %>" SIZE="5" MAXLENGTH="5"></INPUT>
  103.  and 
  104. <INPUT TYPE="text" NAME="upperbound" VALUE="<%= intUpperBound %>" SIZE="5" MAXLENGTH="5"></INPUT>
  105.  
  106. <INPUT TYPE="submit"></INPUT>
  107.  
  108. </FORM>
  109.