newbie: question on rotating quotes

S

Sez1

Hello.

I am trying to set up two rotating quotes on my web page.

I only have a pool of 20 quotes to start with, which I've saved
separately in .js files using document.write to include html formatting.

The two quotes appearing on the page obviously should not be duplicated;
but I'm not sure how to set it up.

I am setting up the variables that govern the quotes in a separate JS
file like this:

var quote1 = Math.random()*20;
quote1 = Math.round(quote1);

var quote2 = Math.random()*5;
quote2 = Math.round(quote2);

Can I simply add an IF statement to this, like:

IF (quote1==quote2 && quote1=20)
{quote1 = quote2-1}
ELSEIF (quote1==quote2 && quote1<20)
{quote1 = quote2+1}


THANKS FOR any pointers on this....
 
L

Lee

Sez1 said:
Hello.

I am trying to set up two rotating quotes on my web page.

I only have a pool of 20 quotes to start with, which I've saved
separately in .js files using document.write to include html formatting.

The two quotes appearing on the page obviously should not be duplicated;
but I'm not sure how to set it up.

I am setting up the variables that govern the quotes in a separate JS
file like this:

var quote1 = Math.random()*20;

That will actually select one of 21 values, 0-20, with zero and twenty being
selected only half as often as any of the other values.

var quote1 = Math.floor(Math.random()*20);

will select twenty values 0-19, and will select them more evenly.

quote1 = Math.round(quote1);

var quote2 = Math.random()*5;
quote2 = Math.round(quote2);

Can I simply add an IF statement to this, like:

IF (quote1==quote2 && quote1=20)
{quote1 = quote2-1}
ELSEIF (quote1==quote2 && quote1<20)
{quote1 = quote2+1}

That would work, but it's actually simpler to do it better.
Think of the quotes as being arranged around a wheel (like a roulette wheel),
with each quote numbered from 0-19.

After choosing the first quote, choose a second random number from 1-19 and
count that many spaces around the circle to find the second quote.

You don't need to test for equality, because you know that you're never counting
all the way around the circle. Here's the code:

var count=20;
var quote1 = Math.floor(Math.random()*count);
var quote2 = (quote1+Math.floor(Math.random()*(count-1)+1))%count;
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue,
10 Aug 2004 03:16:43, seen in Sez1
Hello.

I am trying to set up two rotating quotes on my web page.

I only have a pool of 20 quotes to start with, which I've saved
separately in .js files using document.write to include html formatting.

The two quotes appearing on the page obviously should not be duplicated;
but I'm not sure how to set it up.

I am setting up the variables that govern the quotes in a separate JS
file like this:

var quote1 = Math.random()*20;
quote1 = Math.round(quote1);

var quote2 = Math.random()*5;
quote2 = Math.round(quote2);

Can I simply add an IF statement to this, like:

IF (quote1==quote2 && quote1=20)
{quote1 = quote2-1}
ELSEIF (quote1==quote2 && quote1<20)
{quote1 = quote2+1}

It will work much better if the third "=" character is duplicated.

There is no need to repeat the equality test :
if (q1==q2) { q1==20 ? q1=0 : q1++ }
or if (q1==q2) { q1++ ; q1%=20 }

It would be better to use Random, as in FAQ 4.22, which gives the
numbers you want (with Round, you get one more possibility) ;

q1 = Random(20)
do q2 = Random(5) ; while (q2==q1)
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue,
10 Aug 2004 03:16:43, seen in Sez1
I am trying to set up two rotating quotes on my web page.

I only have a pool of 20 quotes to start with, which I've saved
separately in .js files using document.write to include html formatting.

The two quotes appearing on the page obviously should not be duplicated;
but I'm not sure how to set it up.

Moreover, the following function will return an array containing in
random order M elements, all different, drawn from 1..N. If N-M is not
too small it will be reasonably efficient.

function RndOrdrSubSet(M, N) { var aSet=[], bSet=[], j
if (M>N) M = N
while (M>0) { j = Math.floor(Math.random()*N + 1)
if (!aSet[j]) bSet[--M] = aSet[j] = j }
return bSet }

See just *above* <URL:http://www.merlyn.demon.co.uk/js-randm.htm#NUD>.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top