Javascript generates a random Random Number of up to 13 m bits based on time, and a random number of 13 BITs

Source: Internet
Author: User

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

<Title> Generate a random number </title>
<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> ");



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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.