JavaScript generates M-bit random number max 13-bit _javascript technique based on time

Source: Internet
Author: User
Tags rand

Generates m-bit random numbers based on time, maximum 13-bit random numbers, and does not guarantee that the first number is not 0

Function Ran (m) {
m = m > 13:m;
var num = new Date (). GetTime ();
Return num.tostring (). substring (13-m);
}
Console.log (Ran (5));

According to the random function generated by the math of the random number intercept m, the maximum number of random numbers not more than 16, to ensure that the first is not 0

function rand (m) {
m = m > 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));

Based on the random function of math, there is no limit to the number of digits, first not 0

function Rando (m) {
var = ';
for (var i = 0; i < m i++) {
var val = parseint (Math.random () *10);
if (i = = 0 && val = = 0) {
i--;
Continue;
}
num + = val;
}
return num;
}
Console.log (Rando (5));
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: info-contact@alibabacloud.com 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.