SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've created an array with 5 occurrences. Before processing the array I want to set an index variable to a random value between 0 and 4. I want to do this so I don't always get the same banner starting from a Ad Rotator script when the page is refreshed. Any ideas how I can set the index variable to a random value?

  2. #2
    Say WHA?! goober's Avatar
    Join Date
    Sep 2000
    Location
    United States
    Posts
    1,921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe you can use the Rnd() Function for numbers, but am unsure of it's exact syntax. You'd probably want to look it up on www.4GuysFromRolla.com or www.ASP101.com.

    Hope this helps!
    Sean Killeen [LinkedIn] [Twitter] [Web]

    Warning: Reality.sys corrupted. Universe halted. Reboot? (Y/N)

  3. #3
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Sean. I've used the following and it works fine.

    <SCRIPT language="JavaScript"><!--
    var maxNumber = 1000;
    var randomNumber = Math.round(maxNumber * Math.random());
    //--></SCRIPT>

    I got it from Cnet at

    http://www.builder.com/Programming/S...dir1.Stupid4_6

  4. #4
    Say WHA?! goober's Avatar
    Join Date
    Sep 2000
    Location
    United States
    Posts
    1,921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad you found your answer!

    Good luck in the future.
    Sean Killeen [LinkedIn] [Twitter] [Web]

    Warning: Reality.sys corrupted. Universe halted. Reboot? (Y/N)

  5. #5
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys,

    Just as a note (not very important), but in JavaScript, when you create a random number, you should always "floor it" to create something truly random, rather than round it. Example:

    Code:
    <SCRIPT language="JavaScript"><!-- 
    var maxNumber = 1000; 
    var randomNumber = Math.floor(maxNumber * Math.random()); 
    //--></SCRIPT>
    Althought it might not make sense, it does. This thread explains it. Some people stubbornly told me that's better, but I couldn't figure out why...it was just accepted, but finally we got it:

    http://javascriptcity.com/forums/sho...?threadid=2366

    aDog


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
  •