随机数组生成的意外行为
Unexpected behavior with random array generation
我试图生成一个 10 到 1000 之间的随机数数组,按降序排列。
这是我写的代码:
function GenerateRandomArray(){
var array = [];
for (var i = 0; i < 10; i++) {
array[i] = Math.round(Math.random() * (1000 - 10 + 1) + 10);
}
return array.sort().reverse();
}
当在终端中 运行 时,这是我得到的结果:
new GenerateRandomArray() =>
[ 924, 804, 79, 788, 585, 451, 267, 217, 153, 135 ]
new GenerateRandomArray() =>
[ 869, 697, 647, 59, 458, 30, 291, 157, 112, 111 ]
new GenerateRandomArray() =>
[ 999, 98, 872, 823, 705, 418, 404, 306, 259, 20 ]
new GenerateRandomArray() =>
[ 688, 666, 664, 615, 580, 565, 336, 304, 250, 246 ]
new GenerateRandomArray() =>
[ 912, 906, 759, 690, 673, 481, 429, 355, 19, 103 ]
为什么有些数组格式正确,而有些数组 中间有一个无序数字?
我测试了:
- 将数字转换为字符串
- 访问数组中的无序元素(它给出相同的数字 - 显然)
- 用函数而不是构造函数来做
这不会改变奇怪的结果。
我是不是漏掉了 JS 强制 属性 之类的东西?
谢谢:)
默认情况下,sort
函数按 alphanumeric/alphabetical 顺序排序(即 "string sort")。随着字符串的变化,"aaa" 出现在 "b" 之前,类似地,“111”出现在“2”之前。
要改为按数值排序,您可以提供自己的比较函数。
array.sort(function(a, b) {
return a - b;
});
使用
return array.sort(function(a, b) {
return a - b;
}).reverse();
或简单地:
return array.sort(function(a, b) {
return b - a;
});
文档:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
引用:
The default sort order is according to string Unicode code points.
If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in Unicode code point order. For example, "Banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order.
To compare numbers instead of strings, the compare function can simply subtract b from a.
还有一件事:
这是一个 returns 随机数数组(已排序)的函数,因此不应将其与 new
关键字一起使用,因为您没有将其用作构造函数。另外,个人喜好,我可能将函数命名为 getRandomNumberArray
或 getArrayOfRandomNumbers
.
样本:https://jsfiddle.net/cmfoo49m/2/
function GenerateRandomArray(){
var arr = [];
for (var i = 0; i < 10; i++) {
arr.push(Math.round(Math.random() * 1000));
}
arr.sort(function compareNumbers(a, b) {
return a - b;
});
return arr;
}
我试图生成一个 10 到 1000 之间的随机数数组,按降序排列。
这是我写的代码:
function GenerateRandomArray(){
var array = [];
for (var i = 0; i < 10; i++) {
array[i] = Math.round(Math.random() * (1000 - 10 + 1) + 10);
}
return array.sort().reverse();
}
当在终端中 运行 时,这是我得到的结果:
new GenerateRandomArray() => [ 924, 804, 79, 788, 585, 451, 267, 217, 153, 135 ]
new GenerateRandomArray() => [ 869, 697, 647, 59, 458, 30, 291, 157, 112, 111 ]
new GenerateRandomArray() => [ 999, 98, 872, 823, 705, 418, 404, 306, 259, 20 ]
new GenerateRandomArray() => [ 688, 666, 664, 615, 580, 565, 336, 304, 250, 246 ]
new GenerateRandomArray() => [ 912, 906, 759, 690, 673, 481, 429, 355, 19, 103 ]
为什么有些数组格式正确,而有些数组 中间有一个无序数字?
我测试了:
- 将数字转换为字符串
- 访问数组中的无序元素(它给出相同的数字 - 显然)
- 用函数而不是构造函数来做
这不会改变奇怪的结果。
我是不是漏掉了 JS 强制 属性 之类的东西?
谢谢:)
默认情况下,sort
函数按 alphanumeric/alphabetical 顺序排序(即 "string sort")。随着字符串的变化,"aaa" 出现在 "b" 之前,类似地,“111”出现在“2”之前。
要改为按数值排序,您可以提供自己的比较函数。
array.sort(function(a, b) {
return a - b;
});
使用
return array.sort(function(a, b) {
return a - b;
}).reverse();
或简单地:
return array.sort(function(a, b) {
return b - a;
});
文档:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
引用:
The default sort order is according to string Unicode code points.
If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in Unicode code point order. For example, "Banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order.
To compare numbers instead of strings, the compare function can simply subtract b from a.
还有一件事:
这是一个 returns 随机数数组(已排序)的函数,因此不应将其与 new
关键字一起使用,因为您没有将其用作构造函数。另外,个人喜好,我可能将函数命名为 getRandomNumberArray
或 getArrayOfRandomNumbers
.
样本:https://jsfiddle.net/cmfoo49m/2/
function GenerateRandomArray(){
var arr = [];
for (var i = 0; i < 10; i++) {
arr.push(Math.round(Math.random() * 1000));
}
arr.sort(function compareNumbers(a, b) {
return a - b;
});
return arr;
}