SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Choose random number

    Hello guys,

    I need your help with the following javascriopt code:

    Given an array with numbers I need to randomly choose one number out of the list. Can you guys give me the code to this script?

    Thanks.
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    SitePoint Zealot coothead's Avatar
    Join Date
    Jan 2005
    Location
    Chertsey, London, England
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there DeNasio,

    does this help...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <style type="text/css">
    <!--
    #rnd_num {
        width:108px;
        line-height:66px;
        text-align:center;
        border:3px double #000;
        margin:auto;
     }
    -->
    </style>
    
    <script type="text/javascript">
    <!--
    window.onload=function() {
       num=new Array(33,56,78,100,666,1944,1+','+234+','+567);
       rnd=Math.floor(Math.random()*num.length);
       document.getElementById('rnd_num').firstChild.data=num[rnd];
     }
    //-->
    </script>
    
    </head>
    <body>
    
    <div id="rnd_num">&nbsp;</div>
    
    </body>
    </html>
    coothead

  3. #3
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the hint. Why doesn't the code below work? Doesn't the variable "pollid" in the second script retain the value as defined in the first script?

    Code:
    <script type="text/javascript">
    var num=new Array(10060,10090);
    var rnd=Math.floor(Math.random()*num.length);
    var pollid=num[rnd];
    //-->
    </script>
    <script type="text/javascript" src="http://www.mysite.com/scripts/my.script.php?poll="+pollid+"&popup=1"></script>
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor


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
  •