473,756 Members | 6,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()*1 00;
btn0 = Math.round(btn0 );
var btn1 = Math.random()*1 00;
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>documen t.write('<a href="link1.htm " onMouseOut="MM_ swapImgRestore( )"
onMouseOver="MM _swapImage(\'pi c1\',\'\',\'ima ges/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 16774
JRS: In article <FzdJe.171673$9 A2.88074@edtnps 89>, 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()*1 00;
btn0 = Math.round(btn0 );
var btn1 = Math.random()*1 00;
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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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$t t5.173087@edtnp s90>, 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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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 "helpfulnes s" -
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.java script"....

Aug 10 '05 #5

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

Similar topics

3
1189
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. My problem : When you refresh several times my web site the pictures are "mixted". I mean on some pictures positions, it is some other pictures that are used instead the good ones. It happens at random.
5
3881
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: http://www.aestheticsurgerycenter.com/scripts/postcard/postcard1.shtml You can choose from some cards, add your own text and send it to a friend. This friend get a mail which tell him/her that he/she has a mail and have to click a link. So far everything works just fine. But when the recipient clicks...
6
2107
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, depending on how I write the HTML, I guess I'dneed the script to set style.backgroundImage or src.I'd need to run it on a few elements, something like:setRandomBgImg('thingy1', ('img1a','img1b', ...) )setRandomBgImg('thingy2', ('img2a','img2b',...
3
6078
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 it is not loading the new picture. Can any one help? public void Presentation_Load(object sender, System.EventArgs e)
4
3081
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 image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
0
1528
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 image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
1
1439
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
2973
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 theory you could get the same image 4 times. I quite often get 2 of the same picture come up. What's the easiest way of saying "once an image is assigned to a variable, take it out of the array"? Here's my code so far.. images = new Array(34);...
11
2151
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 one or the other img sets with a simple onclick This works great in Mozilla but in IE I get the error documentnull or not an object, line 95, char 4. I've spent hours on this, but no luck.
0
9271
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9707
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5139
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2664
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.