Please help a JavaScript idiot
June 14, 2006 1:42 PM   Subscribe

Displaying multiple random images via JavaScript?

It's been a long time since I made a new HTML document, and my JavaScript skills were never that great to begin with - mostly at a "monkey see, monkey do" level. My attempts to locate some code to use online have been stymied by my lack of fundamental understanding.

As you can see at this page (, I'm trying to form the number "25" out of 23 square images. I have about 117 images in total, so I'd like the page to randomly select 23 from the 117 and form "25" out of those.

I'm using a randomization JS script that a friend coded for me years and years ago. The script works fine, but when I try to use multiple instances of it on the same page, the functionality breaks down because the randomness is calculated based on the time, and therefore the variable doesn't change enough from image to image to image - at best I can get 3 images out of the 117 to appear.

The script itself is here (

Can anyone please suggest a way I can have each of the 23 displayed images randomly selected independently from the others? I realize that basic probability means a few of the 23 might come up the same, and that's fine with me.

I know my existing code is rough, but this is a work-in-progress.

This is the foundation of an "e-vite" page for a friend's birthday. It's strictly personal, but I'd be happy to pay a small stipend via PayPal to anyone who could produce the code I'm looking for here.

Any help and suggestions will be greatly appreciated. Don't you hate it when you assume something will work fine, and then get to end and realize you're a bonehead?
posted by chudmonkey to Computers & Internet (5 answers total)
Cosine of the time? Cute, but slapworthy. Try using Math.random().
posted by fleacircus at 1:54 PM on June 14, 2006

At line 3, try maybe:

var rand =Math.floor(Math.random()*X)

Where X is the size of your array (i.e., 133). The Math.random() function will pull a random number between 0 and 1. Just multiple that by your upper bound and you'll have a random number between 0 and that bound.
posted by Doofus Magoo at 1:55 PM on June 14, 2006

So, how do I integrate the Math.random function into my existing code? Is it simply a matter of taking something out and putting something different in, or will it require a revamp of the whole kit-and-kaboodle?
posted by chudmonkey at 2:03 PM on June 14, 2006

In 75x75.js, rewrite function randNum as follows:

function randNum (num) {
return Math.floor(Math.random() * num);

Should do it.
posted by profwhat at 2:57 PM on June 14, 2006

In case anyone is curious, the finished implementation is here. (
posted by chudmonkey at 6:26 PM on June 14, 2006

« Older Experiences of intensive NLP courses   |   What's funny about potatoes? Newer »
This thread is closed to new comments.