Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-06-2004, 03:17 AM   PM User | #1
the_bob
New Coder

 
Join Date: Dec 2003
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
the_bob is an unknown quantity at this point
random questions

ok, i've found my huge lack of java scripting knowledge. I have no clue how to use random things. For example, how would you be able to have a list of URLS but only one would pop-up when the site loads? (OnLoad)
the_bob is offline   Reply With Quote
Old 01-06-2004, 03:21 AM   PM User | #2
fredmv
Regular Coder

 
Join Date: Jul 2003
Location: Massachusetts
Posts: 472
Thanks: 0
Thanked 0 Times in 0 Posts
fredmv is an unknown quantity at this point
Code:
<script type="text/javascript">
//<![CDATA[
	var uri =
	[
		'http://www.yahoo.com/',
		'http://www.google.com/',
		'http://www.hotbot.com/'
	];
	
	onload = function()
	{
		window.open(uri[Math.floor(Math.random()*uri.length)]);
	}
//]]>
</script>
fredmv is offline   Reply With Quote
Old 01-06-2004, 09:02 AM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Math.random() function generates a random number between 0 and 1 (with more or less digits decimals - usually 16).

To obtain a random weighed number between 0 and N you simply multiply the genuine random number with the difference (N-0) and round the result

To round you may use different functions such as
Math.round - automatic
Math.floor - rounds down
Math.ceil - rounds up

It looks like using Math.floor or Math.ceil is a better choise than Math.round (they give a "more randomized" weighed numbers). Math.floor has another advantage, see below.

In your example, you need to obtain a random weighed number between 0 and the lenght of your object type (the number of the objects). Why not lenght-1?... Becouse Math.floor will round for instance 2.99999999999 as 2, and never reach the full 3.

So here we are with the explanation for the fredmv's code
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 01-06-2004, 11:54 AM   PM User | #4
the_bob
New Coder

 
Join Date: Dec 2003
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
the_bob is an unknown quantity at this point
fred, thanks for the code, thats great!

and Kor, Thanks for the explanation. Random numbers are probably one of the most commonly used things in java script. I really appreciate it.

but one more question, what does the [CDATA[ in freds code do?

Last edited by the_bob; 01-06-2004 at 11:57 AM..
the_bob is offline   Reply With Quote
Old 01-06-2004, 02:17 PM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Maybe fredmv used an editor with an XML validator, as CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup...
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 01-06-2004, 08:14 PM   PM User | #6
fredmv
Regular Coder

 
Join Date: Jul 2003
Location: Massachusetts
Posts: 472
Thanks: 0
Thanked 0 Times in 0 Posts
fredmv is an unknown quantity at this point
Good explanations Kor (good guess about the XML validator, however, I manually add the CDATA section in scripts or CSS whenever I use embedded code, however, I usually make the code external anyway)...

Last edited by fredmv; 01-16-2004 at 07:03 PM..
fredmv is offline   Reply With Quote
Old 01-06-2004, 08:18 PM   PM User | #7
the_bob
New Coder

 
Join Date: Dec 2003
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
the_bob is an unknown quantity at this point
oh, thanks, both of you
the_bob is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

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

Forum Jump


All times are GMT +1. The time now is 05:11 PM.


Advertisement
Log in to turn off these ads.