JavaScript,生成一个长度为9个数字的随机数
我正在寻找一种高效、优雅的方法来生成长度为9位的JavaScript变量:JavaScript,生成一个长度为9个数字的随机数,javascript,Javascript,我正在寻找一种高效、优雅的方法来生成长度为9位的JavaScript变量: 示例:323760488您可以生成9个随机数字并将它们连接在一起 或者,您可以调用random()并将结果乘以100000000: Math.floor(Math.random() * 1000000000); 由于Math.random()生成了一个介于0和1之间的随机双精度数字,因此您将有足够的精度数字,以便在最低有效位置仍然具有随机性 如果要确保数字以非零数字开头,请尝试: Math.floor(10000000
示例:323760488您可以生成9个随机数字并将它们连接在一起 或者,您可以调用
random()
并将结果乘以100000000:
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;
}
函数leftpad带零(数字、长度)
{
var str=''+数字;
while(str.length
或使用此功能的键盘。数学地板(数学随机()*89999999+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的测试机器)
100万次迭代:~626ms。到目前为止,最快的parseInt是一个本机函数,而不是再次调用数学库。注:见下文
Math.floor(Math.random()*1000000000)
100万次迭代:~1005ms。两个函数调用
String(Math.random()).substring(2,11)
100万次迭代:~2997毫秒。三个函数调用
String(Math.random()).substring(2,11)
而且
parseInt(Math.random()*1000000000)
100万次迭代:~362ms。
注意:如果没有基数参数,parseInt通常被认为是不安全的。请参阅或谷歌“JavaScript:好的部分”。但是,传递给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一行(ish):
var len = 10;
parseInt((Math.random() * 9 + 1) * Math.pow(10,len-1), 10);
步骤:
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);
这将生成10万到999之间的随机值
对于其他方法,由于前导零在某种程度上是一个问题,所以我很难得到可靠的结果 我想试试你的问题。当我运行下面的代码时,它为我工作
<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>
函数getRandomInt(最小值、最大值){
返回Math.floor(Math.random()*(max-min))+min;
}//最大值是独占的,最小值是包含的
$(文档).ready(函数(){
$(“#随机按钮”)。在(“单击”,函数()上){
var randomNumber=getRandomInt(100000000,999999999);
$(“#随机数”).html(随机数);
});
函数rand(len){var x='';
对于(var i=0;i
使用toFixed alows可以设置比默认长度更长的长度(似乎在小数点后生成15-16位数字。如果需要,toFixed可以让您获得更多数字。我知道答案很旧,但我想分享这种方法来生成从0到n的整数或浮点数。请注意点的位置(float case)在边界之间是随机的。该数字是一个字符串,因为现在的限制是9007199254740991
Math.hRandom=函数(位置,浮点=假){
var数=”;
var点=-1;
如果(浮动)点=数学地板(数学随机()*位置)+1;
for(设i=0;i
函数randomCod(){
让代码=”;
设chars='abcdefghijlmnopqrstuvxwz';
设数字='0123456789';
让SpecialCharter='/{}$%&@*/()!-=?';
对于(设i=4;i>1;i--){
让random=Math.floor(Math.random()*99999.toString();
代码+=SpecialCharter[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)])+(数字[random.substring(i,i-1)];
}
code=(code.indexOf(“未定义”)>-1 | | code.indexOf(“NaN”)>-1)?randomCod():code;
返回码;
}
这已经有足够的答案了吗?
我想不会。因此,即使Math.random()
决定返回类似0.000235436
的值,也应该可靠地提供一个9位数的数字:
Math.floor((Math.random() + Math.floor(Math.random()*9)+1) * Math.pow(10, 8))
使用max
exclusive:Math.floor(Math.random()*max);
使用max
inclusive:Math.round(Math.random()*max);
为了生成长度为n的数字字符串,多亏了@nvitaterna,我想出了以下方法:
1 + Math.floor(Math.random() * 9) + Math.random().toFixed(n - 1).split('.')[1]
它防止第一个数字为零。
每次调用时,它都可以生成长度约为50的字符串。长度如何为9位?9位必须是固定长度,不能超过或少于9位。必须处理0.000001234这样的情况,用零填充到9位digits@nobosh:0是一个有效数字,即使在数字的左侧也是如此。如果您想要一个大于等于100000000的数字,则可以不得不这么说(我已经更新了我的答案以涵盖这个案例)。将结果数字转换为字符串。如果长度<9,则在左侧填充零。零是非常好的随机数,不应受到歧视!@nobosh:这取决于,是否允许前导零?例如,00000000 1
是否为有效数字?这些数字将填充为零sweet@user1167442因为Math.random()
产生类似于
Math.floor((Math.random() + Math.floor(Math.random()*9)+1) * Math.pow(10, 8))
1 + Math.floor(Math.random() * 9) + Math.random().toFixed(n - 1).split('.')[1]