473,395 Members | 1,790 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,395 software developers and data experts.

Help with random code...

Ok well what I am trying to do is have

1.) the background color to change randomly with 5 different colors.(change
on page load)

2,) 10 different quotes randomly fadeing in and out in random spots on the
webpage. with a delay timer on them, so they keep changing as the page is
open. Not random each time the page is loaded.

If anyone can help it would be greatly appreaciated, I have tried many of
things and just cant seem to get what I am looking for.

It seems so simple yet so complex...

take the 00's out of email addy to send
Jul 20 '05 #1
10 2473
Virus wrote:
Ok well what I am trying to do is have

1.) the background color to change randomly with 5 different colors.(change
on page load)

2,) 10 different quotes randomly fadeing in and out in random spots on the
webpage. with a delay timer on them, so they keep changing as the page is
open. Not random each time the page is loaded.

If anyone can help it would be greatly appreaciated, I have tried many of
things and just cant seem to get what I am looking for.

It seems so simple yet so complex...

take the 00's out of email addy to send

Put your values in an array...
var quotes_array = new Array();

You can populate the array...
quotes_array[0] = "This is a quote";

Now, you can get a random number, between 1 and 10:
var rand = Math.floor(Math.random() * 10) + 1;

You can use that random number as an index to the array:
alert(quotes_array[rand]);

Does that help?
Brian

Jul 20 '05 #2
Lee
Brian Genisio said:

Virus wrote:
Ok well what I am trying to do is have

1.) the background color to change randomly with 5 different colors.(change
on page load)

2,) 10 different quotes randomly fadeing in and out in random spots on the
webpage. with a delay timer on them, so they keep changing as the page is
open. Not random each time the page is loaded.

If anyone can help it would be greatly appreaciated, I have tried many of
things and just cant seem to get what I am looking for.

It seems so simple yet so complex...

take the 00's out of email addy to send

Put your values in an array...
var quotes_array = new Array();

You can populate the array...
quotes_array[0] = "This is a quote";

Now, you can get a random number, between 1 and 10:
var rand = Math.floor(Math.random() * 10) + 1;

You can use that random number as an index to the array:
alert(quotes_array[rand]);


You'll want the random number to range from zero up
to one less than the number of elements in the array,
which is simply:

var rand = Math.floor(Math.random()*quotes_array.length);

Jul 20 '05 #3
"Brian Genisio" <Br**********@yahoo.com> wrote in message
news:40********@10.10.0.241...
<snip>
Put your values in an array...
var quotes_array = new Array();

You can populate the array...
quotes_array[0] = "This is a quote";
Or an Array literal:-

var quotes_array = [
"This is a quote",
"This is quote 2",
"This is quote 3",
"This is quote 4"
];
Now, you can get a random number, between 1 and 10:
var rand = Math.floor(Math.random() * 10) + 1;
Or:-

var rand = Math.ceil(Math.random() * 9);

But javascript arrays are zero based.
You can use that random number as an index to the array:
alert(quotes_array[rand]);


So basing the random number on the length of the array would save the
two pieces of code getting out of step:-

var rand = Math.floor(Math.random() * quotes_array.length);

Randomly selecting from an Array has a reasonable probability of
repeatedly selecting the same element. It would probably be better to
randomly shuffle the contents of the array and then loop through the
results sequentially.

Richard.
Jul 20 '05 #4
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:
"Brian Genisio" <Br**********@yahoo.com> wrote in message
var rand = Math.floor(Math.random() * 10) + 1;


Or:-

var rand = Math.ceil(Math.random() * 9);


No, Math.random gives numbers in the range [0,1[, i.e., zero is
inclusive. This can give the numbers 0-9 with a very low probability
of 0.
var rand = Math.floor(Math.random() * quotes_array.length);
hich is much better, going on canonical.
Randomly selecting from an Array has a reasonable probability of
repeatedly selecting the same element. It would probably be better to
randomly shuffle the contents of the array and then loop through the
results sequentially.


Probably simpler than avoiding repeats, yes. There is a trick to
avoiding repeats, but I don't know if it's worth it:

First choice (num choices):
var selected = Math.floor(Math.random()*num);

Later choice (num-1 choices to avoid repeat):
var tmp = Math.floor(Math.random()*(num-1));
selected = (tmp >= selected ? tmp+1 : tmp);

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Ok well i understand the background color completly and it works great.
However I dont think i completly explained my problem. I want the background
color to change on each page load ok- and that is done with the code below.
Well then I want text- Plain white text to fade in and out. No, black boxes
around it or anytype of colored boxes or images. Just plain white text to
randomly appear and fade in and out. I saw that script below online and was
trying to make it work but I just couldnt completly understand the language
that well.

So the image program below could work if i could get white text to appear
as an image and have its background transparent or invisable. U understand
what i mean? Thanks again for your time and patients with me :-)
"Virus" <vi*******@cox.net> wrote in message
news:fN8Xb.964$hE.667@fed1read07...
Ok well what I am trying to do is have

1.) the background color to change randomly with 5 different colors.(change on page load)

2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is
open. Not random each time the page is loaded.

If anyone can help it would be greatly appreaciated, I have tried many of
things and just cant seem to get what I am looking for.

It seems so simple yet so complex...

take the 00's out of email addy to send

Jul 20 '05 #6
JRS: In article <br**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Sat, 14 Feb 2004 01:22:51 :-
"Richard Cornford" <Ri*****@litotes.demon.co.uk> writes:

var rand = Math.floor(Math.random() * quotes_array.length);


hich is much better, going on canonical.


In at least one browser, |0 is a little faster than Math.floor. I was
told that, in some Opera, Math.random can give 1, which Math.random()%1
will fix.

If javascript were better designed, Math.random(N) would give a random
integer in 0..(N-1) - Random in Pascal gives 0 <= X < 1 but Random(N)
gives 0 <= n < N.
Randomly selecting from an Array has a reasonable probability of
repeatedly selecting the same element. It would probably be better to
randomly shuffle the contents of the array and then loop through the
results sequentially.


Probably simpler than avoiding repeats, yes. There is a trick to
avoiding repeats, but I don't know if it's worth it:

First choice (num choices):
var selected = Math.floor(Math.random()*num);

Later choice (num-1 choices to avoid repeat):
var tmp = Math.floor(Math.random()*(num-1));
selected = (tmp >= selected ? tmp+1 : tmp);


ISTM that the probability on the second choice of getting the next
higher number will be doubled, unless the first selected was the
highest.

n = -1 // untested
for (j=0; j<2;) { t = Random(N) ; if (t != n) A[j++] = n = t }
A = [] // untested
for (j=0 ; j<K;) { t = Random(N) ; if (!A[t]) { A[t]=t ; j++ }
But the easy general solution is to do a partial shuffle.

<URL:http://www.merlyn.demon.co.uk/js-randm.htm>.

--
© 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.
Jul 20 '05 #7
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
which is much better, going on canonical.
In at least one browser, |0 is a little faster than Math.floor.


But it only works for numbers less than 2^32. For most practical
purposes that is not a problem, but it needs to be said, so doesn't
qualify as canonical :)
I was told that, in some Opera, Math.random can give 1, which
Math.random()%1 will fix.
Correct. I have forgotten the version, but I think it was O5.
That is an error in Opera, though, and shouldn't be used to
complicate the general solution.

To find a random number in the range 0..n-1, this is the most direct
version and, barring implementation errors of the primitives, it
works correctly for all numbers that can be represented:
Math.floor(Math.random()*n);

(Interesting observation: In Opera 7,
Math.random()*Math.pow(2,32)
is always an integer. That means that the random floating point number
is simply a 32-bit random integer divided by 2^32. Multiplying
by only Math.pow(2,31) gave numbers ending in ".5" half the time.
I.e, at most 32 bits worth of randomness in each random number.

In IE and MozFireFox, there are decimals on the result :)

If javascript were better designed, Math.random(N) would give a random
integer in 0..(N-1) - Random in Pascal gives 0 <= X < 1 but Random(N)
gives 0 <= n < N.
I agree. It is the most common use of random number generation, and
should be supported directly.
First choice (num choices):
var selected = Math.floor(Math.random()*num);

Later choice (num-1 choices to avoid repeat):
var tmp = Math.floor(Math.random()*(num-1));
selected = (tmp >= selected ? tmp+1 : tmp);


ISTM that the probability on the second choice of getting the next
higher number will be doubled, unless the first selected was the
highest.


No, all elements that were not the previous choice have an equal
chance. It has no memeory about choices before that, all it prevents
is that the same element is chosen twice in a row.

If the first choice picks number 5 out of 0..7, the second choice
picks a random number between 0 and 6. If it is 5 or above, it adds
one, so the possible outcomes become 0, 1, 2, 3, 4, 6 and 7.
n = -1 // untested
for (j=0; j<2;) { t = Random(N) ; if (t != n) A[j++] = n = t }
This rerolls until it gets a new result. It has expected execution
time O(2 * N/(N-1)), but in the hypothetical worst case, it never
terminates because it keeps rolling the same number.

It won't diverge when using a pseudo-random number generator, because
it won't generate the same number every time and will eventually
(depending on its period) hit the entire phase space. It still makes
analysis a little harder :)
A = [] // untested
for (j=0 ; j<K;) { t = Random(N) ; if (!A[t]) { A[t]=t ; j++ } change to "true", or 0 can never be marked ^

Picks K out of N random numbers, no element twice. Execution time
can be bad. If K>N, it always diverges, but that would be stupid :)

You should store the result somewhere (e.g., B[j++]=t) or use it.

If N=K, it finds a permutation. The expected time for that is
O[n*ln(N)] (not as bad as I feared, although it can be done
in linear time)
But the easy general solution is to do a partial shuffle.


Yes. Finding a random permutation and keep cycling that is often
better than completely random switching (with no immediate repeates).

This problem seems to often come up for people permuting advertisment
banners :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8
JRS: In article <ad**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Sun, 15 Feb 2004 01:37:15 :-
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
In at least one browser, |0 is a little faster than Math.floor.


But it only works for numbers less than 2^32.


2^31.

I was told that, in some Opera, Math.random can give 1, which
Math.random()%1 will fix.


Correct. I have forgotten the version, but I think it was O5.


(5.02..6.01 at least), I have read; and "LRN 20030804 : Opera 4, 5, not
6.05."

(Interesting observation: In Opera 7,
Math.random()*Math.pow(2,32)
is always an integer. That means that the random floating point number
is simply a 32-bit random integer divided by 2^32. Multiplying
by only Math.pow(2,31) gave numbers ending in ".5" half the time.
I.e, at most 32 bits worth of randomness in each random number.


MSIE4 gives, apparently, 53 bits, but I've not checked the cycle length
- 2^53 would take too long. But it might be practical to see whether,
in Opera 7, the cycle length is 2^32 or thereabouts.

--
© 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.
Jul 20 '05 #9
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
MSIE4 gives, apparently, 53 bits, but I've not checked the cycle length
- 2^53 would take too long. But it might be practical to see whether,
in Opera 7, the cycle length is 2^32 or thereabouts.


It seems to be. It does repeat after 2^32 steps (Opera 7.5
preview). Ofcourse, it could have a shorter period that divides 2^32,
but that'll be another test.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10
JRS: In article <pt**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Mon, 16 Feb 2004 08:32:02 :-
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
MSIE4 gives, apparently, 53 bits, but I've not checked the cycle length
- 2^53 would take too long. But it might be practical to see whether,
in Opera 7, the cycle length is 2^32 or thereabouts.


It seems to be. It does repeat after 2^32 steps (Opera 7.5
preview). Ofcourse, it could have a shorter period that divides 2^32,
but that'll be another test.


Since my PC is 300 MHz and its cycle length is probably at least 2^53,
I'll not be testing that myself.

But just before <URL:http://www.merlyn.demon.co.uk/js-randm.htm#AfR> the
resolution of Math.random is determined :-

function Resol() { var j, X, T, M = 0
for (j = 0 ; j < 100 ; j++) { X = Math.random()
T = 0 ; while ((X*=2)%1 > 0) T++ ; if (T>M) M = T }
document.write("to be<tt> ", M, " <\/tt>bits") }

One day, it may be useful to know the resolution and cycle length.

--
© 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.
Jul 20 '05 #11

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

Similar topics

2
by: Martin Ho | last post by:
Hi Everyone, I have this code of Mersenne twister, which produces the random numbers, one of the fastest codes as far as I know to produce random numbers. Anyways, it's writen in c# and I need to...
36
by: Cap'n Ahab | last post by:
I have used VB3 - VB6, so learning all this OO stuff is reasonably new to me (although I looked at Java a few years ago). Anyway, I thought I would write a small class to begin with, with a...
2
by: sgMuser | last post by:
Hi, I am not a good developer of Javascript codes. Needs this help to make some modification to this famous free javascript from Anarchos. i am using this in one of my webpage. What it does is,...
1
by: ArcInversion | last post by:
Hi, I've been using a javascript script to create a dragon that flies across the page. Anyways, I'd like to make it so when you click the dragon it takes you to a new page. Was wondering if anyone...
8
by: GabrielESandoval | last post by:
i currently use the code below to create a slideshow of images. i edited it so that its not as long. i currently have over 20 images i want to change it so that the images dont appear in the...
16
by: BartlebyScrivener | last post by:
I am a mere hobbyist. Spent several hours trying to make a class, because I think this is an occasion where I need one. But I can't make it work. This code "works" (only because of the global c,...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
4
by: UMstudent | last post by:
I'm working on this program for class where I have to an amount of numbers that are randomly picked from 0 - 50. So what I have done is say there are 20 slots in my array so I'm trying to generator...
1
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
9
by: DaiOz | last post by:
Hi guys im doing a course which includes some JavaScript and I need help with the following question please, The code I have so far is below but I don't think its right please help as I need...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.