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 09-02-2009, 01:03 PM   PM User | #1
oceanic
New to the CF scene

 
Join Date: Sep 2009
Location: North West UK
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
oceanic is an unknown quantity at this point
Question Help needed with code for program please

Hi I am new to programming and I am trying to write a piece of code, I need the output to be a whole random number, this is what I have up to now :

<SCRIPT language = "JavaScript">

//generate a random number greater than or equal to 0.0 and less than 50.0
//and display it in an alert dialogue
window.alert("getRoundedRandomNumber: " Math.round + Math.random()*50);

</SCRIPT>
</HEAD>
<BODY>
<STRONG>A test of the random number functions <BR></STRONG>
<FORM NAME = "randomForm">

<INPUT TYPE = "button" NAME = "randomButton" VALUE ="Display Random Number"
ONCLICK = "window.alert(getRoundedRandomNumber(50));">
</FORM>


can anyone advise me where I would put the Math.round function and how to make it work ?

Thanks in advance
oceanic is offline   Reply With Quote
Old 09-02-2009, 01:51 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Sounds like homework.

Code:
<script type = "text/javascript">

function getRoundedRandomNumber(num) {
var randy = Math.floor(Math.random() * num +1);
alert (randy);
}

</script>

<strong>A test of the random number functions <br></strong>

<input type = "button" name = "randomButton" value ="Display Random Number" onclick = "getRoundedRandomNumber(50);">
<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

Use lowercase for tags - <script>, <input> etc.

Math.round() does not produce true random numbers. In this case the numbers 0 and 50 will be chosen less frequently. Other numbers in the range can be arrived at by rounding up or by rounding down - but 50 can only be arrived at by rounding up, and 0 only by rounding down. Use instead Math.floor(Math.random() * num +1). If you want another explanation see:-
http://www.shawnolson.net/a/789/make...om-useful.html

Note the difference between
var randy = Math.floor(Math.random() * num +1); // gives 1 to num
and
var randy = Math.floor(Math.random() * (num +1)) // gives 0 to num


BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.

Last edited by Philip M; 09-02-2009 at 04:07 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
oceanic (09-02-2009)
Reply

Bookmarks

Tags
code, javascript, program

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 02:18 AM.


Advertisement
Log in to turn off these ads.