Is there a limit of the Array at 84?

M

mandelum

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
 
D

denisb

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"
 
M

Michael Winter

(e-mail address removed) 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
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sat, 23 Apr 2005 14:12:18, seen in (e-mail address removed) 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top