need help with random pictures

P

Phillo

Hello,

I'm new at Javascript, and have written a script for a series of random
roll-over button images, but one thing I would like to add is a function
that checks to make sure that there are no duplicates in the randomly
generated variables that choose the pictures.

Can anyone give me a hand with this? One other thing I can't seem to figure
out is how to manage the "onLoad" aspect of caching my roll-over images (DW
has locked the <body onload> function into a template, so I can't mess with
it...). so my rollover graphics are grabbed from the server in "real time"
during mouse-over, which is a bit "goofy" and "slow" in terms of instant
rollover effect. I'm sure that there is a much more efficient way of doing
this, but I've been able to learn a lot by tweaking DW's way of doing
things, so it's not a total loss.

In an external JS file, I set up a series of 10 random variables using the
Math.random and Math.round functions like so:

var btn0 = Math.random()*100;
btn0 = Math.round(btn0);
var btn1 = Math.random()*100;
btn1 = Math.round(btn1); .... etc for 10 buttons


Then in my page, I piggy-back onto Dreamweaver's built-in roll-over
handlers, like so (to write the instances of each rollover image);

<script>document.write('<a href="link1.htm" onMouseOut="MM_swapImgRestore()"
onMouseOver="MM_swapImage(\'pic1\',\'\',\'images/btns/'+btn0+'b.jpg\',1)"><img
src="images/btn/'+btn0+'a.jpg" name="pic1" width="80" height="100"
border="0"></a>');</script>

For this particular page, I'm pretty much stuck with DW's coding because
it's locked into a template that governs my site navigation. Thanks for any
pointers or help ...

Phil
 
D

Dr John Stockton

JRS: In article <FzdJe.171673$9A2.88074@edtnps89>, dated Sun, 7 Aug
2005 01:35:33, seen in Phillo
I'm new at Javascript, and have written a script for a series of random
roll-over button images, but one thing I would like to add is a function
that checks to make sure that there are no duplicates in the randomly
generated variables that choose the pictures.

If they are randomly chosen, then there will be a probability of
duplicates.

To test for duplicates, the most efficient way is to sort and then scan
for a value that equals its immediate predecessor. If none are found,
there were no duplicates. But it's not a good approach to satisfying
your requirement.

In an external JS file, I set up a series of 10 random variables using the
Math.random and Math.round functions like so:

var btn0 = Math.random()*100;
btn0 = Math.round(btn0);
var btn1 = Math.random()*100;
btn1 = Math.round(btn1); .... etc for 10 buttons

Random normally means that all have equal probability; yours do not.
Read the newsgroup FAQ.

To generate a non-repeating sequence, you *can* generate a simple
ordered sequence and then shuffle it; but you *should* deal the numbers
(since the code is shorter and quicker). Read the newsgroup FAQ.
 
P

Phillo

Yeah.

The "long" and the "short" of it. One must dig for the simple jewels.

The short of it, I found, after some digging was this:

- create an array for my entire picture "bank"
- randominze the array (the challenging part)
- if I need 10 random pix, use the first 10 array placeholders as my pic
variables.

Now, was that hard to say?

Phil
 
D

Dr John Stockton

JRS: In article <NO9Ke.194129$tt5.173087@edtnps90>, dated Tue, 9 Aug
2005 22:07:41, seen in Phillo
Yeah.

The "long" and the "short" of it. One must dig for the simple jewels.

The short of it, I found, after some digging was this:

- create an array for my entire picture "bank"
- randominze the array (the challenging part)
- if I need 10 random pix, use the first 10 array placeholders as my pic
variables.

Now, was that hard to say?

Hard to spell, it seems.

The "jewel" you have found is but paste.

Please quote previous messages in the normal Usenet manner, as per
newsgroup FAQ. If you don't, you are liable to be ignored.

Your method will "work"; to work correctly you will need a correct
shuffle. But it is inefficient. Read the newsgroup FAQ.

You did not previously say that you would only be using a subset of the
available images; the optimum algorithm for that case is not necessarily
the same as the optimum for using all of the images.

You should of course not create the names of any images other than
immediately before use; it's best to call your images something like
img1000.gif to img####.gif. Then, having selected a "random" index J in
0..N-1 for the next of the N images, the name is "img" + (1000+J) +
".gif".
 
P

Phillo

The short of it, I found, after some digging was this:
Hard to spell, it seems.

The "jewel" you have found is but paste.

Funny, but the "paste" I found works admirably. Works much better than the
"stern school-teacher approach" that you and many other
hospitality-challenged posters seem to take. If people would take the time
and the stance simply to be helpful, rather than to get onto a podium and
vent their unresolved personal angst - thinly disguised as "helpfulness" -
or provide useless "bible lessons", like "go and read the incredibly boring
thousand-page technical gibberish that passes off as a FAQ, and then you'll
have credibility and enlightenment, my lost son..."

Excuse me, while I go and check SPELLING MISTAKES on all the recent posts,
and assign them the penalty of reading my incredibly boring and irrelevant
"faq" if they demonstrate less pearly wisdom and know-how than my own
venerable holinesss... perhaps I'll forever banish them from my sacred
"venting fountain", known as "comp.lang.javascript"....
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top