Javascript generates a random Random Number of up to 13 m bits based on time, and a random number of 13 BITs
Generates a random m-bit number based on the time. The maximum random number is 13 digits, and the first digit cannot be zero.
function ran(m) {m = m > 13 ? 13 : m;var num = new Date().getTime();return num.toString().substring(13 - m);}console.log(ran(5));
Based on the random number generated by the random function of Math, the random number is intercepted by m bits. the maximum number of random numbers generated cannot exceed 16 bits, and the first digit cannot be 0.
function rand(m) {m = m > 16 ? 16 : m;var num = Math.random().toString();if(num.substr(num.length - m, 1) === '0') {return rand(m);}return num.substring(num.length - m);}console.log(rand(5));
Generated Based on the random function of Math. There is no limit on the number of digits and the first digit is not 0.
function rando(m) {var num = '';for(var i = 0; i < m; i++) {var val = parseInt(Math.random()*10, 10);if(i === 0 && val === 0) {i--;continue;}num += val;}return num;}console.log(rando(5));
Use Javascript programming to generate a random number of less than 10
<Html>
<Head>
<Title> Generate a random number </title>
</Head>
<Body>
<Script type = "text/javascript">
For (I = 1; I <11; I ++ ){
// Math. random () is to generate 0 ~ Random number between 1
Var ran = Math. random () * 10; // generate 0 ~ Random Number between 10
Document. write ("nth" + I + "random number:" + ran + "<br> ");
}
</Script>
</Body>
</Html>
Use javascript to generate a random number ranging from 0 to 10.
Document. write (parseInt (10 * Math. random (); // output 0 ~ A random integer between 10