Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: random

  1. #1
    New Coder
    Join Date
    Feb 2004
    Location
    london
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random

    Hi Everyone,
    Its a glum morning here in london, think the summer is on its way out already, time to hibernate I guess.
    Anyway, can anyone tell me whats wrong with the code below, i'm using the following code to output:

    Code:
    <script language="JavaScript">document.write('article[choice]'));</script>
    Code:
    var range = 6; 
    // Number of alternative nodes 
    var article = new Array(selectnode); 
    // Array to hold nodes
    article[0] = "Content1"; 
    article[1] = "Content2"; 
    article[2] = "Content3"; 
    article[3] = "Content4"; 
    article[4] = "Content5"; 
    article[5] = "Content6"; 
    
    function pickRandom(range) { 
    	if (Math.random) return Math.round(Math.random() * (range-1)); 
    	else { var now = new Date(); return (now.getTime() / 1000) % range; } 
    	} 
    
    var choice = pickRandom(selectnode);
    thanks in advance,
    99 times out of 10!

  • #2
    New Coder
    Join Date
    Aug 2006
    Location
    Montreal, Canada
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm... well, all i see that is (perhaps) wrong is your declaration of
    var range = 6;
    should that be var selectnode = 6; ?
    and you have too many brackets on your document.write()

    that's all i see.

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can't figure out what your overall goal is....
    Are you trying to load a random "content" from an array? From what I can tell, aside from the extra paran's that jpalfree points out, your document.write command will only output: article[choice]
    You'd need to remove it from the quotes to get it to display what is stored in the array.
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    And article[choice] in the document.write does not want to be in quotes, also use Math.floor() in the random statement.

    Something like this


    PHP Code:
    <script type="text/javascript">

    var 
    article = new Array(); 
    article[0] = "Content1"
    article[1] = "Content2"
    article[2] = "Content3"
    article[3] = "Content4"
    article[4] = "Content5"
    article[5] = "Content6";

    range=article.length

    function pickRandom() { 
    if(
    Math.random){
    return 
    Math.floor(Math.random() * range)
    }
    else{
    var 
    now = new Date();
    return (
    now.getTime() / 1000) % range;



    </script>

    <script type="text/javascript">
    document.write(article[pickRandom()]);
    </script> 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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