PDA

View Full Version : random number and document.write question


carrieelisabeth
09-17-2003, 05:19 PM
hey, new to the board and jscripting...

what im trying to accomplish is a script that selects a random number, between 0 and 6. i then want to call that random value within two image tags in my code. the ulimate result will be two random images, corresponding to one another (because i have named the images accordingly), called in two different positions on the page.

i have it working in IE but in mozilla and NS the images dont show up at all (not even the red x). i have a feeling its probably my syntax since this my first attempt ever at writing jscript so hopefully its a quick fix:

var randomnumber=Math.floor(Math.random()*6);

and

document.write("<img src='images/groupa" +[randomnumber]+ ".jpg'>");

is what ive come up with. i want the image tag to look like (where 1 is the random #):

<img src="images/groupa1.jpg">

thanks.

lavalamp
09-17-2003, 06:56 PM
Just take the square brackets out of this:

document.write("<img src='images/groupa" +[randomnumber]+ ".jpg'>");

to get this:

document.write('<img src="images/groupa' +randomnumber+ '.jpg">');

I've also swopped the types of quotes around, this is purely because I code in XHTML and in that all attribute values should be enclosed in double quotes.


Edit: You could even do this:

document.write('<img src="images/groupa' +(Math.floor(Math.random()*6))+ '.jpg">');

carrieelisabeth
09-17-2003, 07:10 PM
thanks! id seen quotes both ways so wasnt sure which was actually correct - i guess both work?

i tried both suggestions but neither one seems to be working in NS or mozilla. is it something with the random # script that isnt compatible?

incidentally, im not sure the second suggestion would do what i want which is to have a single value passed to both locations. if i run the random # script in each image tag, the potential for two different #s to show up exists.

cheesebag
09-17-2003, 07:18 PM
<script type="text/javascript" language="javascript">

var randomnumber = Math.floor(Math.random() * 7);
document.write('<img src="images/groupa' + randomnumber + '.jpg">');

</script>
.......
.......
.......
<script type="text/javascript" language="javascript">

document.write('<img src="images/groupb' + randomnumber + '.jpg">');

</script>

Math.floor() rounds downwards (floor?!) so, you'll need to specify a seed one digit higher than the highest you need.

Goes in the <body>, naturally....;)

fredmv
09-17-2003, 07:20 PM
Math.floor() rounds downwards (floor?!)


So true - I always thought of it like that too - And Math.ceil() rounds upwards, ceiling!

Pretty cool. I wonder if that's what Netscape was thinking when they named those methods... :D

lavalamp
09-17-2003, 07:22 PM
var randomnumber = Math.floor(Math.random() * 7);

What if the random number is 1?

In any case I made this, and tested it on Moz 1.4, and as low as NN 4.79. Works fine on both.

Edit: Re-uploaded attachment with Math.round instead of Math.floor.

Choopernickel
09-17-2003, 07:31 PM
Originally posted by lavalamp
What if the random number is 1?

var randomnumber = Math.floor(Math.random() * 6.9);

That should keep randomnumber from being anything but an integer in the range 0-6.

liorean
09-17-2003, 07:35 PM
The random number generated should be 0>x>1, and should thus be neither 0 nor 1.

cheesebag
09-17-2003, 07:37 PM
Math.random() returns a pseudorandom number between zero and one. That's between as in 'not including'...:rolleyes:

fredmv
09-17-2003, 07:37 PM
Originally posted by lavalamp
What if the random number is 1?


<script type="text/javascript">
var n = Math.floor( Math.random() * 10 );

if( n == 0 )
{
n = 1;
}
</script>

carrieelisabeth
09-17-2003, 07:39 PM
again, thanks everyone.

since the two images i need are going to be in different, nested divs, i cant put everything in the same block of script like the example in the zip file. i must be doing something wrong when i break the code into three parts since it still wont work :confused:

now im even confused about the random # part :eek:

lavalamp
09-17-2003, 07:45 PM
OK, how's this. I've even changed back to Math.floor.

carrieelisabeth
09-17-2003, 07:58 PM
youre my hero. thanks! :thumbsup:

(also, i created an image called groupa0.jpg in case the random # was 1)

fredmv
09-17-2003, 08:20 PM
Originally posted by carrieelisabeth
(also, i created an image called groupa0.jpg in case the random # was 1)

You didn't need to - Check out my previous post!

carrieelisabeth
09-17-2003, 09:03 PM
i had already created the image before seeing your post. im going to rename it and add your script just because you took the time to help, tho :p

lavalamp
09-17-2003, 09:37 PM
Happy to help. :)