JavaScript,生成一个长度为9的随机数

我正在寻找一种高效、优雅的方法来生成一个长度为9位数的 JavaScript 变量:

示例: 323760488

136628 次浏览

您可以生成9个随机数字并将它们连接在一起。

或者,你可以调用 random()并将结果乘以1000000000:

Math.floor(Math.random() * 1000000000);

由于 Math.random()生成一个介于0和1之间的随机双精度数字,因此您将有足够的精度数字,以便在最不重要的位置仍然具有随机性。

如果您想确保您的数字以非零数字开头,请尝试:

Math.floor(100000000 + Math.random() * 900000000);

或者用零填充:

function LeftPadWithZeros(number, length)
{
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}


return str;
}

或垫使用此 内联“诡计”


var number = Math.floor(Math.random()*899999999 + 100000000)


屏幕抓取这个页面:

为什么不直接从 Math.random()字符串表示中提取数字呢?

Math.random().toString().slice(2,11);
/*
Math.random()                         ->  0.12345678901234
.toString()              -> "0.12345678901234"
.slice(2,11)  ->   "123456789"
*/

(要求每个 javascript 实现 Math.random()的精度至少为小数点后9位)

我发现了三种按效率顺序排列的方法: (运行 Firefox 7.0 Win XP 的测试机)

parseInt(Math.random()*1000000000, 10)

100万次迭代: ~ 626ms。到目前为止,最快的 parseInt 是一个本机函数,而不是再次调用 Math 库。注意: 见下文。

Math.floor(Math.random()*1000000000)

100万次迭代: ~ 1005ms。两个函数调用。

String(Math.random()).substring(2,11)

100万次迭代: ~ 2997ms。三个函数调用。

还有..。

parseInt(Math.random()*1000000000)

100万次迭代: ~ 362ms。 注意: parseInt 通常在没有基数参数的情况下使用是不安全的。参见 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt或 google“ JavaScript: The Good Part”。但是,传递给 parseInt 的参数似乎永远不会以“0”或“0x”开头,因为输入首先被乘以100000000。YMMV.

还有..。

function getRandom(length) {


return Math.floor(Math.pow(10, length-1) + Math.random() * 9 * Math.pow(10, length-1));


}

GetRandom (9) = > 234664534

一行(大概) :

var len = 10;
parseInt((Math.random() * 9 + 1) * Math.pow(10,len-1), 10);

步骤:

  • 我们生成一个满足 1 ≤ x < 10的随机数。
  • 然后,我们乘以 Math.pow(10,len-1)(长度为 len的数字)。
  • 最后,parseInt()删除小数。

如果您想生成随机电话号码,那么通常禁止从零开始。 这就是为什么你应该结合一些方法:

Math.floor(Math.random()*8+1)+Math.random().toString().slice(2,10);

这将产生100000000至999999999之间的随机

使用其他方法时,我在获得可靠的结果方面遇到了一些麻烦,因为前导零在某种程度上是个问题。

我想尝试一下你的问题。当我运行以下代码时,它对我起作用了。

<script type="text/javascript">


function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
} //The maximum is exclusive and the minimum is inclusive
$(document).ready(function() {


$("#random-button").on("click", function() {
var randomNumber = getRandomInt(100000000, 999999999);
$("#random-number").html(randomNumber);
});


</script>
function rand(len){var x='';
for(var i=0;i<len;i++){x+=Math.floor(Math.random() * 10);}
return x;
}


rand(9);
Math.random().toFixed(length).split('.')[1]

使用 toFix 可以设置长度大于默认值(似乎在小数后面生成15-16位数字)。如果你需要的话,ToFix 可以让你得到更多的数字。

我知道这个问题的答案已经很老了,但是我想分享这种生成 从0到 n 的整数或浮点数的方法。注意,点的位置(浮点大小写)在边界之间是随机的。这个数字是一个字符串,因为 MAX _ SAFE _ INTEGER现在的限制是9007199254740991

Math.hRandom = function(positions, float = false) {


var number = "";
var point = -1;


if (float) point = Math.floor(Math.random() * positions) + 1;


for (let i = 0; i < positions; i++) {
if (i == point) number += ".";
number += Math.floor(Math.random() * 10);
}


return number;


}
//integer random number 9 numbers
console.log(Math.hRandom(9));


//float random number from 0 to 9e1000 with 1000 numbers.
console.log(Math.hRandom(1000, true));

function randomCod(){


let code = "";
let chars = 'abcdefghijlmnopqrstuvxwz';
let numbers = '0123456789';
let specialCaracter = '/{}$%&@*/()!-=?<>';
for(let i = 4; i > 1; i--){


let random = Math.floor(Math.random() * 99999).toString();
code += specialCaracter[random.substring(i, i-1)] + ((parseInt(random.substring(i, i-1)) % 2 == 0) ? (chars[random.substring(i, i-1)].toUpperCase()) : (chars[random.substring(i, i+1)])) + (numbers[random.substring(i, i-1)]);
}


code = (code.indexOf("undefined") > -1 || code.indexOf("NaN") > -1) ? randomCod() : code;




return code;
}

这已经有足够的答案了吗?
我想也是。因此,即使 Math.random()决定返回类似于 0.000235436的值,它也应该可靠地提供一个9位数的数字:

Math.floor((Math.random() + Math.floor(Math.random()*9)+1) * Math.pow(10, 8))
  1. max独家报道: Math.floor(Math.random() * max);

  2. 包括 max: Math.round(Math.random() * max);

为了生成一个长度为 n 的数字字符串,多亏了@nvitaterna,我想出了这个方法:

1 + Math.floor(Math.random() * 9) + Math.random().toFixed(n - 1).split('.')[1]

它可以防止第一个数字为零。 它可以在每次调用时生成长度约为50的字符串。

var number = Math.floor(Math.random() * 900000000) + 100000000

对于一些 10字符

Math.floor(Math.random() * 9000000000) + 1000000000

来自 https://gist.github.com/lpf23/9762508

这个答案是为那些希望生成 10数字号码(没有国家代码)的人准备的