SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    ASP: Random numbers?

    Ergh... Me and math in ASP, eh?

    Aright, random number 1 to 10... How?

    If I remember correctly there's a UBound and LBound or something in VB, but I doubt that I'm remembering correctly...
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  2. #2
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here ya go:
    Code:
    Int((Rnd * TopValue) + lowValue)
    So it would be
    Code:
    Int((Rnd * 10) + 1)

  3. #3
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    http://server2.tacf.org/play/

    It always spits out the same number:

    If Top is 10, it's 8.
    If Top is 6, it's 5.
    If Top is 20, it's 15.
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  4. #4
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Apologises!!

    I made a post and then realised it wasn't relevant and deleted it, but J had already replied. First time I've deleted a post and I won't be doing it again. Sorry.

    Here's my original post:

    Jeremy, use the Randomize statement before calling the Rnd function

    From VBS5.5 doc
    Randomize Statement
    Initializes the random-number generator.

    Randomize [number]

    The number argument can be any valid numeric expression.

    Remarks
    Randomize uses number to initialize the Rnd function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value.

    If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.
    New post:
    Re-reading the the Randomize spec I think the seed value will be set regardless.

    I'm not sure what the problem is (if there is one) with the code dhtmlgod posted but the following has works for me.

    Code:
    Randomize
    intLength = 10
    intRandom = CInt((Rnd * 1000)Mod intLength) + 1

  5. #5
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <%
    'doRan Function
    '******************************************************'Creates a random number between UHigh and LLow
    'Usage response.write doRan(6,1)
    
    function doRan(UHigh,LLow)
    	Randomize
    	intLength = UHigh
    	doRan = CInt((Rnd * 1000)Mod intLength) + LLow
    end function
    '******************************************************%>
    result: http://server2.tacf.org/play
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  6. #6
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jeremy,

    That bit of code I posted will just work for number ranges 1 - x.

    Try the following function. There's some code to test it as well (client side vbs mind.) There's no error checking so you may want to add some checking on the params.

    Code:
    <script type="text/vbscript">
    
    Function GetRandomNo(intHigh, intLow)
    	Randomize
    	GetRandomNo = CInt((Rnd * 1000)Mod (intHigh - intLow + 1)) + intLow
    End Function
    
    'Set the high and low ranges
    intHigh = 20
    intLow = 1
    
    'Create array to hold numbers
    intArraySize = intHigh - intLow
    ReDim arrNumbers(1, intArraySize)
    
    'Initialize numbers array
    'The first element contains the number and the second contains
    'the number of times the number is generated during the random loop
    intNumber = intLow
    For j = 0 To (intArraySize)
    	arrNumbers(0, j) = intNumber
    	arrNumbers(1, j) = 0
    	intNumber = intNumber + 1
    Next
    
    'Create loop to generate random numbers
    For i = 1 To 1000
    			
    	'Get the random number
    	intRndNumber = GetRandomNo(intHigh, intLow)
    	
    	'Increment the count for the generated number
    	intIndex = intRndNumber - intLow
    	intCount = arrNumbers(1, intIndex) 
    	arrNumbers(1, intIndex) = intCount + 1
    Next
    
    'Write out the numbers and the associated count
    For i = 0 To UBound(arrNumbers, 2)
    	Document.Write "Number: " & arrNumbers(0, i) & " Count: " & arrNumbers(1, i) & "<br />"
    Next
    
    </script>
    Last edited by shane; Oct 10, 2001 at 12:39.

  7. #7
    SitePoint Member
    Join Date
    Sep 2001
    Location
    Calgary, Alberta, Canada
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ??

    You People Confuse Me All You Have To Do Is

    <%
    Randomize
    Number = (Int((10) * Rnd + 1))
    %>
    Last edited by Luther2001; Oct 11, 2001 at 15:13.

  8. #8
    SitePoint Wizard iTec's Avatar
    Join Date
    May 2001
    Location
    Sydney, Australia
    Posts
    2,243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    all i ever use is
    <%
    Dim Rndnumber
    Randomize
    Rndnumber = int(Rnd * 24)+1
    %>

    And i know it works! dont wuite get what all the other stuff you guys are on about is supposed to do that the above lines cant do.

  9. #9
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Luther: Yours doesn't assign the value to a variable.
    iTec: Mine is a function, encapsulated and reusable. It does the same thing though.
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  10. #10
    SitePoint Wizard iTec's Avatar
    Join Date
    May 2001
    Location
    Sydney, Australia
    Posts
    2,243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Jeremy W.
    iTec: Mine is a function, encapsulated and reusable. It does the same thing though.
    Just a quickie, but what is the point in making it a function? i mean does it make it quicker (hardly resource intensive)

    i just dont see a benefit in making 3 lines of code a function.

  11. #11
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,123
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I can reuse it again and again and again.

    For instance, I need 6 distinct random numbers for what I'm doing. Which is easier repeating doRan(6,1) 6 times, or repeating all 3 lines 6 times?

    Also, I can reuse the function in other sites, thus building up a library of useful "bits" which allow a site to be quickly developed and deployed.
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •