Jump to content

Math.random


Aviway
 Share

Recommended Posts

If i use the function - "Math.random" with the "loop - for"(30 times), Can i "sum" all the result of the "Math.random" together????

  <script language="JavaScript">  <!--	var x=33;	var y=45;	var str="";	for (loop=1;loop<30;loop++ )	{tempature=Math.round(Math.random()* (y - x) + x);	str+=tempature+"\n";	}	sum=str+tempature;	alert(sum)  //-->  </script>

Link to comment
Share on other sites

total = 0;for (loop=1;loop<30;loop++ )  temperature=Math.round(Math.random()* (y - x) + x);  total += temperature}alert(total)

Edited by Ingolme
Link to comment
Share on other sites

TANX.and if i want to define for each time that the "if" function is true a "string"

	   var total2000 = 0;	   var total2001 = 0;	   var avg00="";	   var avg01="";	   var str="";		for (loop=1;loop<30;loop++ )		{temperature=Math.round(Math.random()* (45 - 33) + 33);		total2000 += temperature;		avg00=Math.round(total2000)/30;		}		for (loop=1;loop<30;loop++ )		{temperature=Math.round(Math.random()* (45 - 33) + 33);		total2001 += temperature;		avg01=Math.round(total2001)/30;		if (temperature>avg00)			{str+=temperature+"\n";			 }		}		alert("July 2000 AVG tempature is: "+avg00+"\n"+"July 2001 AVG tempature is: "+avg01+"\n"+str);

that the alert will be like that:day "n" = 43

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...