Velocity Reviews > Is there a limit of the Array at 84?

# Is there a limit of the Array at 84?

mandelum@gmail.com
Guest
Posts: n/a

 04-23-2005
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"

denisb
Guest
Posts: n/a

 04-23-2005
<(E-Mail Removed)> 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 !
' `) /
|\_ =="

Michael Winter
Guest
Posts: n/a

 04-23-2005
http://www.velocityreviews.com/forums/(E-Mail 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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Dr John Stockton
Guest
Posts: n/a

 04-24-2005
JRS: In article <(E-Mail Removed) .com>,
dated Sat, 23 Apr 2005 14:12:18, seen in news:comp.lang.javascript,
(E-Mail 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.

--
<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.