Javascript 文本框中自动生成的数字

Javascript 文本框中自动生成的数字,javascript,jquery,asp.net,json,Javascript,Jquery,Asp.net,Json,我使用JS代码在文本框中生成15位数字,并希望将生成的数字减少到5或6位。请帮帮我 现在=新日期(); 随机数=''; randomNum+=Math.round(Math.random()*9); randomNum+=Math.round(Math.random()*9); randomNum+=now.getTime(); window.onload=函数(){ document.getElementById(“txt_usrid”).value=randomNum; } 您可以使用s

我使用JS代码在文本框中生成15位数字,并希望将生成的数字减少到5或6位。请帮帮我


现在=新日期();
随机数='';
randomNum+=Math.round(Math.random()*9);
randomNum+=Math.round(Math.random()*9);
randomNum+=now.getTime();
window.onload=函数(){
document.getElementById(“txt_usrid”).value=randomNum;
}

您可以使用
substr


现在=新日期();
随机数='';
randomNum+=Math.round(Math.random()*9);
randomNum+=Math.round(Math.random()*9);
randomNum+=now.getTime();
window.onload=函数(){
document.getElementById(“txt_usrid”).value=randomNum.substr(0,5);//HERE;)
}

最有效的方法是从
.getTime()
中检索最后3位数字,并将它们附加到2个随机整数中。由于这些数字与当前日期的毫秒相关,它们应该意味着生成的数字看起来是随机的——尽管显然不是真正随机的。试试这个:

now=新日期();
随机数='';
randomNum+=Math.round(Math.random()*9);
randomNum+=Math.round(Math.random()*9);
randomNum+=now.getTime().toString().slice(-3);
window.onload=函数(){
document.getElementById(“txt_usrid”).value=randomNum;
}

尝试使用
数组#切片

now=新日期();
随机数='';
randomNum+=Math.round(Math.random()*9);
randomNum+=Math.round(Math.random()*9);
randomNum+=now.getTime().toString().slice(-4);
window.onload=函数(){
document.getElementById(“txt_usrid”).value=randomNum;
}


现在=新日期();
随机数='';
randomNum+=Math.round(Math.random()*9);
randomNum+=Math.round(Math.random()*9);
randomNum+=Math.round(now.getTime()/100000000);//它创建了自1970/01/01以来的毫秒数,因此应该更少
randomNum=Math.round(Math.random()*randomNum);
window.onload=函数(){
document.getElementById(“txt_usrid”).value=randomNum;
}
now=新日期();
随机数='';
randomNum+=Math.round(Math.random()*9);
randomNum+=Math.round(Math.random()*9);
randomNum+=now.getTime().toString().slice(-3);
window.onload=函数(){
document.getElementById(“txt_usrid”).value=randomNum;
}

尝试方法:

now=新日期();
随机数='';
randomNum+=Math.round(Math.random()*9);
randomNum+=Math.round(Math.random()*9);
randomNum+=now.getTime();

log(randomNum.substring(5,11))问题在于,最后三位数字几乎不会改变,因为它们与当前年份相关。1。OP想要一个5位数的数字,而不是6位数。2.问题是,最后三位数字与本年度相关时几乎不会改变。@Rorymcrossan请看那里
我想将生成的数字减少到5或6
。OP wants
5 r 6
问题是,最后三位数字与本年度相关时几乎不会改变。添加此行
randomNum=Math.round(Math.random()*randomNum)@Rorymcrossant Hanks Rory McCrossan。很好,没问题。如果有帮助,不要忘记接受答案。检查此链接。希望它能起作用。