If statement random choice

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fran7
    New Member
    • Jul 2006
    • 229

    If statement random choice

    Hi,

    I hope this is a simple thing.

    This code works perfectly, but I want to have 5 random choices,(or more)
    How do I make this choose from 5 random choices?

    Code:
    <%
    randomize()
    r=rnd()
    if r>0.5 then
    <%
      <a href='http://www.w3schools.com'>W3Schools.com!</a>
    <%
    Else
    %>
      <a href='http://www.refsnesdata.no'>Refsnesdata.no!</a>
    <%
    end if
    %>
    It would be great if anyone knew.
    Thanks
    Richard
    Last edited by fran7; Nov 1 '07, 06:11 PM. Reason: new idea
  • markrawlingson
    Recognized Expert Contributor
    • Aug 2007
    • 346

    #2
    There are several parts to this really.

    1) Generating a random number
    2) Using the generated random number to select a resource
    3) Keeping track of, or supplying, some sort of interval to base this off of as well as keeping track of the resource itself so that the program knows what it needs to display for the next x interval rather than generating a new one every time the page loads.

    Randomizing is easy.

    [CODE=ASP]

    Function RandomNumber(iF rom,iTo)
    Randomize
    RandomNumber = Int(Rnd * iTo)+iFrom
    End Function
    'The above code is an easy to use function and can be called multiple times on a page as in the following...
    Response.Write RandomNumber(1, 500)
    'Which will generate a random number between 1 and 500 and display it on the page
    [/CODE]

    Number 2 is a little more complicated. You have to decide where to get your information. You could store it in a database, or a text file, or you could simply keep everything in your code and use an array.

    [CODE=ASP]
    aGreeting = Split("Hi! My name is jack!|Hi! My Name is Joe!|Hi! My Name is Tom!|Hi! My name is George!","|")

    Response.Write aGreeting(Rando mNumber(LBound( aGreeting),UBou nd(aGreeting)))
    [/CODE]

    The above code sets an array of greetings, then writes out a randomly selected greeting by referring to the array's index (0-4 in this case). We pass our RandomNumber function from above to select a random index from the array using the Upper and Lower boundaries of the array as the to and from fields in the function. This essentially means, "generate a random number from 0-4" in this case.

    And finally, number 3. If you want to display something randomly every x period, you would have base that period off of something. If it's a date.. (like every week, every month, every dec 25th) that's fine, but if it's something more obscure like every 9 days, or every 3 hours - you would have to store the last interval somewhere - like in a database or .txt file or xml file. And even then, unless you want the page to select a new random every single time the page is loaded, you would have to keep track of, somewhere, the selected random which is supposed to be displayed for the next x interval and subsequently replace that value once the x interval is up.

    For instance - say the code above selects "Hi! My name is jack!" - we would store than somewhere then display it on the page for the next x days, or if the interval is up we would generate a new random, save it to our data store, and display that instead for the next x days.

    Does this make sense?

    Sincerely,
    Mark

    Comment

    • fran7
      New Member
      • Jul 2006
      • 229

      #3
      Dear Mark, That makes absolute sense. I think the easiest for me is every 10th of month and an array.
      Just one thing.
      The If clause I have above, is that an array of sorts, as that was the only way I could get (html with asp inside) to work? I cannot get html in the normal array format to work
      Thanks
      Richard

      Comment

      • markrawlingson
        Recognized Expert Contributor
        • Aug 2007
        • 346

        #4
        No that's not an array. That's just an IF...ELSE statement. An array is defined as, basically, numerous variables or data combined into one variable and referred to by their index.

        EX:

        [CODE=ASP]
        MyArray = Array("My","Nam e","Is","Mar k")
        [/CODE]

        Is an array with 4 indice. In other words it has 4 items within the "MyArray" variable. These are referred to by their index starting with 0, as in the example below.

        [CODE=ASP]
        Response.Write MyArray(0) 'My
        Response.Write MyArray(1) 'Name
        Response.Write MyArray(2) 'Is
        Response.Write MyArray(3) 'Mark
        [/CODE]

        So in other words, an array is just a type of variable that can hold multiple pieces of information. Rather than having a string variable with "My name is mark" - we've set it into an array so it now has 4 values "my", "name", "is", and "mark".

        It gets even more complicated when you get into multi-dimensional arrays, which are basically like small database tables, but I won't get into that.

        Sincerely,
        Mark

        Comment

        Working...