Is there a limit of the Array at 84?

Discussion in 'Javascript' started by mandelum@gmail.com, Apr 23, 2005.

  1. Guest

    I have a page with lots of random bgs and my script has worked fine
    utill I reached the number of 85 in the Array. When there is 85 in the
    array my page is just white. Why is this, and is there any way to solve
    the problem?

    var randnum = Math.random();
    var inum = 93;
    // Change this number to the number of images you are using.
    var rand1 = Math.round(randnum * (inum-1)) + 1;
    images = new Array
    images[1] = "mm.jpg"
    images[2] = "camotex.jpg"
    images[3] = "meo.jpg"
    ....
    images[83] = "maubin.gif"
    images[84] = "galen4.gif"
    images[85] = "that2.gif
    images[86] = "noise2.gif"

    thanks a lot in advice
     
    , Apr 23, 2005
    #1
    1. Advertising

  2. denisb Guest

    <> wrote:
    > I have a page with lots of random bgs and my script has worked fine
    > utill I reached the number of 85 in the Array. When there is 85 in the
    > array my page is just white. Why is this, and is there any way to solve
    > the problem?
    > var randnum = Math.random();
    > var inum = 93;
    > // Change this number to the number of images you are using.
    > var rand1 = Math.round(randnum * (inum-1)) + 1;
    > images = new Array
    > images[1] = "mm.jpg"
    > images[2] = "camotex.jpg"
    > images[3] = "meo.jpg"
    > ...
    > images[83] = "maubin.gif"
    > images[84] = "galen4.gif"
    > images[85] = "that2.gif

    ____________________________î
    missing " (quote) here

    > images[86] = "noise2.gif"



    --
    @@@@@
    E -00 comme on est very beaux dis !
    ' `) /
    |\_ =="
     
    denisb, Apr 23, 2005
    #2
    1. Advertising

  3. wrote:

    [snip]

    > var randnum = Math.random();
    > var inum = 93;
    > // Change this number to the number of images you are using.
    > var rand1 = Math.round(randnum * (inum-1)) + 1;


    OK, that just seems silly to me.

    function randomInt(n) {
    return Math.floor((Math.random() % 1) * n);
    }

    Then when calling, you'd pass the length of the array:

    if(document.body && document.body.style) {
    document.body.style.backgroundImage = 'url('
    + images[randomInt(images.length)] + ')';
    }

    This would return a value in the range 0 <= x < length-1. That is, from
    the first to last elements of the array.

    Notice that you truncate, not round, the random number. Rounding alters
    the distribution of values, making the resulting number less 'fair'.

    > images = new Array
    > images[1] = "mm.jpg"


    Arrays have zero-based indicies in ECMAScript.

    [snip]

    > images[85] = "that2.gif


    You missed a closing quote. A syntax-highlighting editor is good for
    catching these errors, as is JSLint.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Apr 23, 2005
    #3
  4. JRS: In article <>,
    dated Sat, 23 Apr 2005 14:12:18, seen in news:comp.lang.javascript,
    posted :
    >I have a page with lots of random bgs and my script has worked fine
    >utill I reached the number of 85 in the Array. When there is 85 in the
    >array my page is just white. Why is this, and is there any way to solve
    >the problem?
    >
    > var randnum = Math.random();
    > var inum = 93;
    > // Change this number to the number of images you are using.
    > var rand1 = Math.round(randnum * (inum-1)) + 1;
    > images = new Array
    > images[1] = "mm.jpg"
    > images[2] = "camotex.jpg"
    > images[3] = "meo.jpg"
    >...
    > images[83] = "maubin.gif"
    > images[84] = "galen4.gif"
    > images[85] = "that2.gif
    > images[86] = "noise2.gif"


    Better to write

    function Random(x) { return Math.floor(x*Math.random()) } // see FAQ

    var images = ["mm.jpg", "camotex.jpg", "meo.jpg", ..., // 0..
    "maubin.gif", "galen4.gif", "that2.gif", "noise2.gif"] // with added "

    var index = Random(images.length)


    Math.round will not give an even distribution; the end values will be of
    half-probability.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Apr 24, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iblix
    Replies:
    4
    Views:
    3,472
    Gary Milton
    Apr 16, 2004
  2. Ben Jessel
    Replies:
    2
    Views:
    6,172
    Sudsy
    Nov 26, 2003
  3. Guy
    Replies:
    8
    Views:
    16,432
    Jukka K. Korpela
    Dec 6, 2003
  4. Replies:
    1
    Views:
    1,088
    Victor Bazarov
    Jun 28, 2005
  5. Ketchup
    Replies:
    1
    Views:
    250
    Jan Tielens
    May 25, 2004
Loading...

Share This Page