<pre class="brush:js;toolbar:false">&lt;script&gt; functionGetRandomNum(Min,Max) { varRange=Max-Min; varRand=Math.random(); return(Min+Math.round(Rand*Range)); } varnum=GetRandomNum(1,10); alert(num); &lt;/script&gt; varchars=[&#39;0&#39;,&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;,&#39;6&#39;,&#39;7&#39;,&#39;8&#39;,&#39;9&#39;,&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;,&#39;E&#39;,&#39;F&#39;,&#39;G&#39;,&#39;H&#39;,&#39;I&#39;,&#39;J&#39;,&#39;K&#39;,&#39;L&#39;,&#39;M&#39;,&#39;N&#39;,&#39;O&#39;,&#39;P&#39;,&#39;Q&#39;,&#39;R&#39;,&#39;S&#39;,&#39;T&#39;,&#39;U&#39;,&#39;V&#39;,&#39;W&#39;,&#39;X&#39;,&#39;Y&#39;,&#39;Z&#39;]; functiongenerateMixed(n){ varres=&quot;&quot;; for(vari=0;i&lt;n;i++){ varid=Math.ceil(Math.random()*35); res+=chars[id]; } returnres; }</pre><p>1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1)</p><p>2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分。</p><p>3.Math.round(num); 参数num为一个数值,函数结果为num四舍五入后的整数。</p><p>Math:数学对象,提供对数据的数学计算。</p><p>Math.random(); 返回0和1间(包括0,不包括1)的一个随机数。</p><p>Math.ceil(n); 返回大于等于n的最小整数。</p><p>用Math.ceil(Math.random()*10);时,主要获取1到10的随机整数,取0的几率极小。</p><p>Math.round(n); 返回n四舍五入后整数的值。</p><p>用Math.round(Math.random());可均衡获取0到1的随机整数。</p><p>用Math.round(Math.random()*10);时,可基本均衡获取0到10的随机整数,其中获取最小值0和最大值10的几率少一半。</p><p>Math.floor(n); 返回小于等于n的最大整数。</p><p>用Math.floor(Math.random()*10);时,可均衡获取0到9的随机整数。</p>
T:0.007753s,M:246.02 KB
返回顶部 留言