HighDots Forums  

Re: A java script from that need a little bit of adjustment - Couldyou help?

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: A java script from that need a little bit of adjustment - Couldyou help? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Thomas Schodt
 
Posts: n/a

Default Re: A java script from that need a little bit of adjustment - Couldyou help? - 05-03-2004 , 12:37 PM






This belongs in: comp.lang.javascript
Followup-to: comp.lang.javascript

Norberto Rivera wrote:

Quote:
Greetings.

I download this java script and it does work. Actually, it is great
for my purpose. Just a little complain: It will be great if it will
open the ads randomly. Right now, it will open then in the order you
see bellow.

I don't know much about java. Could anyone modify this easely enough
to do the random thing? Thanks. email: Norberto_Rivera (AT) yahoo (DOT) com
================================================== =====================

script language="JavaScript1.1"
!--
/*
JavaScript Image slideshow:
By Website Abstraction (www.wsabstract.com)
and Java-scripts.net (www.java-scripts.net)
*/

var slideimages=new Array()
var slidelinks=new Array()

function slideshowimages(){
for (i=0;i<slideshowimages.arguments.length;i++){
slideimages[i]=new Image()
slideimages[i].src=slideshowimages.arguments[i]
}
}

function slideshowlinks(){
for (i=0;i<slideshowlinks.arguments.length;i++)
slidelinks[i]=slideshowlinks.arguments[i]
}

function gotoshow(){
if (!window.winslide||winslide.closed)
winslide=window.open(slidelinks[whichlink])
else
winslide.location=slidelinks[whichlink]
winslide.focus()
}
//--
/script

a href="javascript:gotoshow()"><img src="img1.gif" name="slide"
border=0></a

script
!--
//configure the paths of the images, plus corresponding target links
slideshowimages("anuncios/ANUNCIATEAQUI.gif",
"anuncios/jazzlatino.gif", "anuncios/SIGNSRUSPR.gif")
slideshowlinks("http://www.signsruspr.com",
"http://http://www.geocities.com/norberto95aolcom/JazzLatino.html",
"http://www.signsruspr.com")

//configure the speed of the slideshow, in miliseconds
var slideshowspeed=15000
var whichlink=0
var whichimage=0

function slideit(){
if (!document.images)
return
document.images.slide.src=slideimages[whichimage].src
whichlink=whichimage

if (whichimage<slideimages.length-1)
whichimage++
else
whichimage=0
setTimeout("slideit()",slideshowspeed)
}
slideit()
//--
/script

Reply With Quote
  #2  
Old   
Brian Genisio
 
Posts: n/a

Default Re: A java script from that need a little bit of adjustment - Couldyouhelp? - 05-03-2004 , 01:17 PM






Thomas Schodt wrote:

Quote:
This belongs in: comp.lang.javascript
Followup-to: comp.lang.javascript

Norberto Rivera wrote:

Greetings.

I download this java script and it does work. Actually, it is great
for my purpose. Just a little complain: It will be great if it will
open the ads randomly. Right now, it will open then in the order you
see bellow.

I don't know much about java. Could anyone modify this easely enough
to do the random thing? Thanks. email: Norberto_Rivera (AT) yahoo (DOT) com
================================================== =====================

script language="JavaScript1.1"
!--
/*
JavaScript Image slideshow:
By Website Abstraction (www.wsabstract.com)
and Java-scripts.net (www.java-scripts.net)
*/

var slideimages=new Array()
var slidelinks=new Array()

function slideshowimages(){
for (i=0;i<slideshowimages.arguments.length;i++){
slideimages[i]=new Image()
slideimages[i].src=slideshowimages.arguments[i]
}
}

function slideshowlinks(){
for (i=0;i<slideshowlinks.arguments.length;i++)
slidelinks[i]=slideshowlinks.arguments[i]
}

function gotoshow(){
if (!window.winslide||winslide.closed)
winslide=window.open(slidelinks[whichlink])
else
winslide.location=slidelinks[whichlink]
winslide.focus()
}
//--
/script

a href="javascript:gotoshow()"><img src="img1.gif" name="slide"
border=0></a

script
!--
//configure the paths of the images, plus corresponding target links
slideshowimages("anuncios/ANUNCIATEAQUI.gif",
"anuncios/jazzlatino.gif", "anuncios/SIGNSRUSPR.gif")
slideshowlinks("http://www.signsruspr.com",
"http://http://www.geocities.com/norberto95aolcom/JazzLatino.html",
"http://www.signsruspr.com")

//configure the speed of the slideshow, in miliseconds
var slideshowspeed=15000
var whichlink=0
var whichimage=0

function slideit(){
if (!document.images)
return
document.images.slide.src=slideimages[whichimage].src
whichlink=whichimage

if (whichimage<slideimages.length-1)
whichimage++
else
whichimage=0
setTimeout("slideit()",slideshowspeed)
}
slideit()
//--
/script
That is correct... Your code has nothing to do with Java, this is
Javascript. They are completely different.

Read the FAQ: http://www.jibbering.com/faq/

You need to look at the Math object. It has a random() function that
returns a random number between 0 and 1.

So, for instance, you might try doing something like this:

whichimage = Math.round(Math.random() * 1009) % slideimages.length;

Here, we create a random number, multiply it against a significantly
large prime number, and round it to an integer. We then mod it
(basically a remainder) from the lengths of your slide array. This will
always present a pseudo-random number between 0 and slideimages.length -
1.

Some may claim that it is not truey random (they are correct), but it
should be random enough for your needs.

Actual use in your code is left as an exercise for the OP.

Brian



Reply With Quote
  #3  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: A java script from that need a little bit of adjustment - Couldyou help? - 05-05-2004 , 08:53 AM



JRS: In article <40967eae$1 (AT) 10 (DOT) 10.0.241>, seen in
news:comp.lang.javascript, Brian Genisio <BrianGenisio (AT) yahoo (DOT) com> posted
at Mon, 3 May 2004 13:17:02 :
Quote:
Read the FAQ: http://www.jibbering.com/faq/

You need to look at the Math object. It has a random() function that
returns a random number between 0 and 1.
Not so. It returns a number X such that 0 <= X <= 1, and the difference
between that and what you wrote should not be disregarded. A bug in
certain browser releases means that one needs to allow for the
possibility of it returning 1.0; I'm not sure whether the buggy versions
can return X > 1.0.

Quote:
So, for instance, you might try doing something like this:

whichimage = Math.round(Math.random() * 1009) % slideimages.length;

Here, we create a random number, multiply it against a significantly
large prime number, and round it to an integer. We then mod it
(basically a remainder) from the lengths of your slide array. This will
always present a pseudo-random number between 0 and slideimages.length -
1.
A bad suggestion. It generates a distribution which is not uniform,
except for the case of one image only. Consider the case of 670 images;
the first half will have approximately twice the frequency of the second
half.


You, too, should read the FAQ, which gives a mathematically sound
solution providing a uniform distribution (and only needs a %1 in the
right place to protect against the aforementioned bug).


Those who try to reinvent the wheel have a duty to check its performance
against those of existing standard wheels before recommending their
invention to the public.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.


Reply With Quote
  #4  
Old   
Brian Genisio
 
Posts: n/a

Default Re: A java script from that need a little bit of adjustment - Couldyouhelp? - 05-05-2004 , 04:18 PM



Dr John Stockton wrote:

Quote:
So, for instance, you might try doing something like this:

whichimage = Math.round(Math.random() * 1009) % slideimages.length;

Here, we create a random number, multiply it against a significantly
large prime number, and round it to an integer. We then mod it
(basically a remainder) from the lengths of your slide array. This will
always present a pseudo-random number between 0 and slideimages.length -
1.


A bad suggestion. It generates a distribution which is not uniform,
except for the case of one image only. Consider the case of 670 images;
the first half will have approximately twice the frequency of the second
half.


You, too, should read the FAQ, which gives a mathematically sound
solution providing a uniform distribution (and only needs a %1 in the
right place to protect against the aforementioned bug).


Those who try to reinvent the wheel have a duty to check its performance
against those of existing standard wheels before recommending their
invention to the public.

Yeah, sorry. I forgot it was in the faq. My suggestion was a quick
one, without thinking much about it.

I made an assumption of a low number of images when I chose the number
1009... For an image set as large as 670 images, a much higher number
would be necessary for the distribution to be even.

This obviously is not a solution that supports maintainability. A
developer does not want to change the multiplier every time the image
set grows.

I did say "for instance, you might try doing something like"... I should
have been a bit more clear, and said something like "Here is an
off-the-head idea demonstrating the use of"... That way, my intent
would have been more clear.

Still, for the case of random images showing on a web site,
random-number theory is usually not necessary. The suggestion I posted
would have worked *OK*.

Brian



Reply With Quote
  #5  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: A java script from that need a little bit of adjustment -Couldyou help? - 05-05-2004 , 04:32 PM



Brian Genisio <BrianGenisio (AT) yahoo (DOT) com> writes:

Quote:
I made an assumption of a low number of images when I chose the number
1009... For an image set as large as 670 images, a much higher number
would be necessary for the distribution to be even.
No number, except a whole multiple of the number of images, will make
the distribution even. So just use the number of images.

What did you expect to gain from using a prime number? I can't see
any advantage to doing that.

Quote:
This obviously is not a solution that supports maintainability. A
developer does not want to change the multiplier every time the image
set grows.
Using the number of images is a lot easier to maintain, especially
if you have them in an array

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #6  
Old   
Brian Genisio
 
Posts: n/a

Default Re: A java script from that need a little bit of adjustment -Couldyouhelp? - 05-05-2004 , 04:40 PM



Lasse Reichstein Nielsen wrote:

Quote:
Using the number of images is a lot easier to maintain, especially
if you have them in an array

/L
Yup... thats what I said... or at least I tried to say it

Brian



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.