# Math.random

## 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>`

##### 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
##### 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

##### Share on other sites

and if i want to define for each time that the "if" function is true a "string"
huh?BTW, that loop is going to run 29 times, not 30. It will run when loop = 1..29.