1.ランダムに4桁の乱数を生成します
<スクリプト言語= "javascript">/*** 4ビットランダム数をランダムに生成*http://www.yulu.vevb.com*/document.write(parseint(10*math.random())); // output random integer document.write(math.floor(math.random()*10+1)); //出力ランダム整数関数rndnum(n){var rnd = ""; for(var i = 0; i <n; i ++)rnd+= math.floor(math.random()*10); Rnd;} document.write(rndnum(4)); //指定された数字のランダム数のランダムな整数</script>2。指定されたデータ範囲の乱数をランダムに生成します
1)1から任意の値まで始めます
parseint(math.random()*上限+1);
2)任意の値から任意の値への開始
<script> parseint(math.random()*(上限低下+1)+下限); function frandomby(under、over){switch(arguments.length){case 1:return parseint(math.random()*under+1);ケース2:parseint(math.random()*(オーバーアンダー1) + under)を返します。デフォルト:0を返します。 }} document.write(frandomby(1,100)); //指定された範囲での乱数のランダム整数</script> //確立されたテキストボックスのルールに従って異なる値をお願いします[拡張] <script> window.onload = function(){var o = document.getelementsBytagname( 'input'); o [0] .value = frandomby(1,10); o [1] .value = frandomby(11,20); o [2] .value = frandomby(1,100); o [3] .value = frandomby(51,100); } < /scrip> 1-10:<input type = "text" /> <br /> 11-20:<入力タイプ= "text" /> <br /> 1-100:<input type = "text" /> <br /> 51-100:<input type = "text" /> <br />>3。拡張例:
<html> <head> <title>乱数を生成するMath-Example www.vevb.com </title> </head> <body> <script Language = "javascript"> "text/javascript">合計= 0for(i = 1; i <= 5000; i ++){num = math.random();合計+= num}平均=合計/5000;平均= math.round(平均*1000)/1000; document.write( "<h1>平均:"+平均+"</h1>")</script> </body> </html>JSで乱数(指定された範囲)を生成するための上記の例コードは、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。