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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random generator throws 'undefined' number occasionally

    Hello all! I'm completely new here so I hope I'm doing this right and posting this in the appropriate subforums - if not, my apologies!

    Anyways, on to my actual problem. I am using the following script on a website to display a different image every time one visits/refreshes. The problem is, sometimes, instead of one of the images I have defined, it will come up with a broken image. I have tried to find something about this kind of thing in tutorials and such, but I have been unable to find anything on the subject. So - here's the code I'm using -

    Code:
    <script type="text/javascript"> 
    <!-- 
    var rand=Math.round(Math.random()*6); 
    var img=new Array(6); 
    img[0]="layout/bottomright/00.gif"; 
    img[1]="layout/bottomright/01.gif"; 
    img[2]="layout/bottomright/02.gif"; 
    img[3]="layout/bottomright/03.gif"; 
    img[4]="layout/bottomright/04.gif"; 
    img[5]="layout/bottomright/05.gif";  
    
    document.write("<img src=\"" + img[rand] + "\" />"); 
    //--> 
    </script>
    Because I have extremely limited knowledge of JavaScript, I am not sure where the problem lies. (I made this script following a tutorial a long time ago and it worked completely fine then - I only changed the number in the "var rand" line to match the number of images I'm using this time, and the issue was present right from the start. I changed "var img" a lot in attempts to fix the issue - like I said, little knowledge, so I was just trying things - but it made no difference.) A more savvy friend of mine pointed out that the generator throws an "undefined" number occasionally, but she didn't know why.

    So... any help? I hope it's not too complex an issue. Thanks in advance!
    Last edited by A.T.; 06-22-2010 at 08:40 AM. Reason: problem was solved :)

  • #2
    New Coder
    Join Date
    Jun 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    0
    Thanked 6 Times in 6 Posts
    The randomizer creates a number 6, which is not defined.
    Decreasing *6 to *5 will solve the problem.

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Candan View Post
    The randomizer creates a number 6, which is not defined.
    Decreasing *6 to *5 will solve the problem.
    That worked! I thought that line told the script the amount of images to choose from and the final image on the list would never show up if I lowered it. I guess it's exactly the other way around from what I thought, haha. Thanks a bunch!

  • #4
    New Coder
    Join Date
    Jun 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    0
    Thanked 6 Times in 6 Posts
    I guess a 'cleaner' way to fix this problem would be:
    Code:
    <script type="text/javascript"> 
    <!-- 
    var img=new Array(); 
    img[0]="layout/bottomright/00.gif"; 
    img[1]="layout/bottomright/01.gif"; 
    img[2]="layout/bottomright/02.gif"; 
    img[3]="layout/bottomright/03.gif"; 
    img[4]="layout/bottomright/04.gif"; 
    img[5]="layout/bottomright/05.gif";  
    
    document.write("<img src=\"" + (Math.floor(Math.random()*img.length )) + "\" />"); 
    //--> 
    </script>
    This one allows you to add more entries in the array, without having to change the script.

  • #5
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    You wouldn't need to edit anything, just add an img[6], img[7], etc. line? How nifty! Thanks, I will defenitely write that down for future use.

  • #6
    New Coder
    Join Date
    Jun 2010
    Location
    The Netherlands
    Posts
    53
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by A.T. View Post
    You wouldn't need to edit anything, just add an img[6], img[7], etc. line? How nifty! Thanks, I will defenitely write that down for future use.
    Yep, thats exactly the only thing you'll have to do.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    And the *REAL* problem in your original code was here:
    Code:
    var rand=Math.round(Math.random()*6);
    You need to use Math.floor().

    Math.round() will *NOT* produce a correct distribution.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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