473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

need help with random pictures

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
Aug 7 '05 #1
4 16744
JRS: In article <FzdJe.171673$9A2.88074@edtnps89>, dated Sun, 7 Aug
2005 01:35:33, seen in news:comp.lang.javascript, Phillo
<he****@helper.com> posted :
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 8 '05 #2
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
Aug 9 '05 #3
JRS: In article <NO9Ke.194129$tt5.173087@edtnps90>, dated Tue, 9 Aug
2005 22:07:41, seen in news:comp.lang.javascript, Phillo
<he****@helper.com> posted :
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".

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 10 '05 #4
>>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.


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

Aug 10 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Seb | last post by:
Hello, I have a (french) web site under PLONE2. Every thing is ok (concerning Zope /PLONE2) , but since few days I get a "very strange problem". I'm using Zope 2.7.0 with Apache under FreeBSD. ...
5
by: Bernard | last post by:
Hi, I have a problem with a CGI script (Perl) on a Win2000 server. The script is for sending E-cards and was written by Jason Maloney:...
6
by: lucy | last post by:
Hello comp.lang.js.I need a script to rotate a background image on each newpage load, given the id of the element and a list (array?)of 2 or more image paths ("img1.jpg", "img2.jpg", ...).Actually,...
3
by: VenuGopal | last post by:
hi, i have a WELCOME screen. here i have a PICTURE BOX. there are 8 pictures that go into this Picture Box. At every 5 seconds i want to RANDOMLY load a new Picture. this is what i am using but...
4
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
0
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
1
by: philleep | last post by:
Hi, How do i do a random picture selector for a page in php. Basically i want to pic 3 pictures from a pool of 40 and display these on screen. Cheer, Philleep
4
toxicpaint
by: toxicpaint | last post by:
Hi, can anyone give me a hand. I'm currently displaying 4 random images at the top of a page. I did this using an array of 35 pictures and then writing them to page. The problem I have is that in...
11
by: bdbeames | last post by:
What I have is a is two sets of images,(Weather, optical) there is about 30 images of is each set. I have script that will rotate through the images on the main page. The user is able to view...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.