Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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)

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    New Coder
    Join Date
    Dec 2003
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh, thanks, both of you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •