如何在設置隨機取值在0-1之外的,而不是僅僅限于0-1之間的?在代碼中的如何修改呢?
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html?xmlns="http://www.w3.org/1999/xhtml"> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/> <title>無標題文檔</title> <script?type="text/javascript"> ???function?change(){ ???????document.getElementById("daan").value=Math.random(document.getElementById("shuru").value);} ???????function?arry(){ ???????document.getElementById("shuzi").value=Math.round(document.getElementById("daan")??.value?)????} </script> <style?type="text/css"> *{font-size:18px;?color:#F00;} #shuru{background:#93C;} button{background:#0F0;} #daan{background:#FF0;} </style> </head> ? <body> ??<form> ????<input?type="text"?placeholder="請輸入數值"?value=""??id="shuru"/> ????<button?type="button"?onclick="change()">取隨機值</button> ????<input?type="text"?placeholder="輸出答案"?value=""??id="daan"/> ????<button?type="button"?onclick="arry()">四舍五入</button> ????<input?type="text"?placeholder="最終答案"?value=""??id="shuzi"/> ??</form> </body> </html>
得出的結果如下:點擊取隨機值的時候回去一個隨機值,但是取到的隨機值永遠是在0-1之間的,那么怎么才能在設置才可以取到0-1之外的數值,比如說2-8之間的數值呢?
2016-10-12
random()默認沒有參數,所以變換區間可以用Math方法,Math.random()*(max?-?min)?+?min ? ? ? ?
2016-10-12
2到8就用round*6+2
2016-10-12
你可以乘數字