Javascript 使用子串生成 4 位随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29640432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
generate 4 digit random number using substring
提问by aviate wong
I am trying to execute below code:
我正在尝试执行以下代码:
var a = Math.floor(100000 + Math.random() * 900000);
a = a.substring(-2);
I am getting error like undefined is not a functionat line 2, but when I try to do alert(a), it has something. What is wrong here?
我undefined is not a function在第 2 行遇到错误,但是当我尝试这样做时alert(a),它有一些东西。这里有什么问题?
回答by Sumner Evans
That's because ais a number, not a string. What you probably want to do is something like this:
那是因为a是一个数字,而不是一个字符串。你可能想要做的是这样的:
var val = Math.floor(1000 + Math.random() * 9000);
console.log(val);
Math.random()will generate a floating point number in the range [0, 1) (note that 1 is excluded from the range).- Multiplying by 9000 results in a range of [0, 9000).
- Adding 1000 results in a range of [1000, 10000).
- Flooring chops off the decimal value to give you an integer. Note that it does not round.
Math.random()将在 [0, 1) 范围内生成一个浮点数(注意 1 被排除在范围之外)。- 乘以 9000 会得到 [0, 9000) 的范围。
- 添加 1000 结果在 [1000, 10000) 的范围内。
- 地板砍掉十进制值给你一个整数。请注意,它不是圆形的。
General Case
一般情况
If you want to generate an integer in the range [x, y), you can use the following code:
如果要生成[x, y)范围内的整数,可以使用以下代码:
Math.floor(x + (y - x) * Math.random());
回答by Alexander Rydningen
This will generate 4-digit random number (0000-9999) using substring:
这将使用子字符串生成 4 位随机数 (0000-9999):
var seq = (Math.floor(Math.random() * 10000) + 10000).toString().substring(1);
console.log(seq);
回答by Isaac Stevens
$( document ).ready(function() {
var a = Math.floor(100000 + Math.random() * 900000);
a = String(a);
a = a.substring(0,4);
alert( "valor:" +a );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
回答by Gaurav Sharma
$(document).ready(function() {
var a = Math.floor((Math.random() * 9999) + 999);
a = String(a);
a = a.substring(0, 4);
});
回答by Balaji Ts
// It Will Generate Random 5 digit Number & Char
const char = '1234567890abcdefghijklmnopqrstuvwxyz'; //Random Generate Every Time From This Given Char
const length = 5;
let randomvalue = '';
for ( let i = 0; i < length; i++) {
const value = Math.floor(Math.random() * char.length);
randomvalue += char.substring(value, value + 1).toUpperCase();
}
console.log(randomvalue);
回答by Ciprian
Your ais a number. To be able to use the substringfunction, it has to be a stringfirst, try
你的a是一个数字。为了能够使用该substring功能,它必须是string第一个,尝试
var a = (Math.floor(100000 + Math.random() * 900000)).toString();
a = a.substring(-2);
回答by renakre
You can get 4-digit this way .substring(startIndex, length), which would be in your case .substring(0, 4). To be able to use .substring()you will need to convert ato string by using .toString(). At the end, you can convert the resulting output into integer by using parseInt:
您可以通过这种方式获得 4 位数字.substring(startIndex, length),这就是您的情况.substring(0, 4)。为了能够使用,.substring()您需要a使用.toString(). 最后,您可以使用以下命令将结果输出转换为整数parseInt:
var a = Math.floor(100000 + Math.random() * 900000)
a = a.toString().substring(0, 4);
a = parseInt(a);
alert(a);
回答by Shelly
The problem is that ais a number. You cannot apply substringto a number so you have to convert the number to a string and then apply the function.
问题是这a是一个数字。您不能应用于substring数字,因此您必须将数字转换为字符串,然后应用该函数。
DEMO: https://jsfiddle.net/L0dba54m/
演示:https: //jsfiddle.net/L0dba54m/
var a = Math.floor(100000 + Math.random() * 900000);
a = a.toString();
a = a.substring(-2);

