...

View Full Version : Resolved random generator throws 'undefined' number occasionally



A.T.
06-21-2010, 02:22 PM
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 -


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

Candan
06-21-2010, 03:38 PM
The randomizer creates a number 6, which is not defined.
Decreasing *6 to *5 will solve the problem.

A.T.
06-21-2010, 03:43 PM
The randomizer creates a number 6, which is not defined.
Decreasing *6 to *5 will solve the problem.

That worked! :thumbsup: 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!

Candan
06-21-2010, 04:02 PM
I guess a 'cleaner' way to fix this problem would be:


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

A.T.
06-21-2010, 04:12 PM
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.

Candan
06-21-2010, 04:18 PM
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.

Old Pedant
06-22-2010, 12:12 AM
And the *REAL* problem in your original code was here:

var rand=Math.round(Math.random()*6);


You need to use Math.floor().

Math.round() will *NOT* produce a correct distribution.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum