Following up on this answer for creating an array of specified length, I executed the below to get a corresponding result but filled with random numbers, instead of zeros.
按照这个答案创建一个指定长度的数组,我执行下面的操作得到一个相应的结果,但是用随机数字填充,而不是零。
var randoms = Array(4).fill(Math.floor(Math.random() * 9));
Well, mathematically speaking it's random, all right. But I'd like the randomness to be visible within the array and not only between runs, of course. Stupid computer... Don't do what I say. Do what I want!
好吧,从数学上讲它是随机的,好的。但是我希望随机性在数组中可见,当然不仅仅是在运行之间。愚蠢的电脑......不要做我说的话。做我想要的!
I can iterate and put it my random (and varying) values. But I wonder, of pure curiosity, if it's possible to get the right result with a one-liner, like above, MatLab-style. Do I need to call eval(function()...)? I've heard a lot of bad things about eval...
我可以迭代并将其作为随机(和变化)值。但我怀疑,纯粹的好奇心,是否有可能通过像MatLab这样的单线程获得正确的结果。我需要调用eval(function()...)吗?我听说过很多关于评估的坏事......
Note that the above produces code like this:
请注意,上面生成的代码如下:
[7, 7, 7, 7]
[3, 3, 3, 3][7,7,7,7] [3,3,3,3]
etc. while I want something like
等我想要的东西
[1, 2, 3, 4]
[4, 3, 8, 4][1,2,3,4] [4,3,8,4]
5 个解决方案
#1
17
What does Array#fill
do?
Array#fill做什么?
According to MDN
据MDN称
The
fill()
method fills all the elements of an array from a start index to an end index with a static value.fill()方法使用静态值将数组的所有元素从起始索引填充到结束索引。
You can use Function#apply
, Array#map
, Math.floor()
, Math.random()
.
您可以使用Function#apply,Array#map,Math.floor(),Math.random()。
In ES6, Array#from
and Arrow function can be used.
在ES6中,可以使用Array#from和Arrow函数。
Array.from({length: 6}, () => Math.floor(Math.random() * 9));
Array.apply(null, Array(6)).map(() => Math.floor(Math.random() * 9));
Array.apply(null,Array(6))。map(()=> Math.floor(Math.random()* 9));
var randomArr = Array.from({length: 6}, () => Math.floor(Math.random() * 9));
document.getElementById('result').innerHTML = JSON.stringify(randomArr, 0, 4); // For demo only
<pre id="result"></pre>
In ES5:
在ES5中:
Array.apply(null, Array(6)).map(function(item, index){
return Math.floor(Math.random() * 9);
});
var randomArr = Array.apply(null, Array(6)).map(function(item, index){
return Math.floor(Math.random() * 9)
});
document.getElementById('result').innerHTML = JSON.stringify(randomArr, 0, 4);
<pre id="result"></pre>
What is Array.apply(null, Array(n))
? Can new Array(n)
used here?
什么是Array.apply(null,Array(n))?可以使用新的Array(n)吗?
Both the above code create new array of six elements, each element having value as undefined
. However, when used new
syntax, the created array is not iterable. To make the array iterable, Array.apply(null, Array(6))
syntax is used.
上面的代码都创建了六个元素的新数组,每个元素的值都是undefined。但是,使用新语法时,创建的数组不可迭代。要使数组可迭代,使用Array.apply(null,Array(6))语法。
If you have lodash included on page, it's really easy.
如果你在页面上有lodash,那真的很容易。
_.times(6, _.random.bind(0, 100))
^ - Number of elements in array
^ - Random number range min
^^^ - Random number range max
Note: This answer is inspired from Colin Toh's blog
注意:这个答案的灵感来自Colin Toh的博客
#2
6
var randoms = Array(4).fill(Math.floor(Math.random() * 9));
This line of code will create a list of 4 of the same number because fill
takes a single value and repeats it for the length of the list. What you want to do is run the random number generator each time:
这行代码将创建一个由4个相同数字组成的列表,因为fill只接受一个值并在列表长度内重复它。你想要做的是每次运行随机数生成器:
var makeARandomNumber = function(){
return Math.floor(Math.random() * 9);
}
var randoms = Array(5).fill(0).map(makeARandomNumber);
console.log(randoms)
// => [4, 4, 3, 2, 6]
https://jsfiddle.net/t4jtjcde/
https://jsfiddle.net/t4jtjcde/
#3
3
Short and simple ES6 approach -
简短的ES6方法 -
// randomly generated n = 4 length array 0 <= array[n] <= 9
var randoms = Array.from({length: 4}, () => Math.floor(Math.random() * 10));
Enjoy!
请享用!
#4
1
I wonder if it's possible to get the right result with a one-liner...
我想知道是否有可能通过单线程获得正确的结果......
var randoms = [...Array(4)].map(虚 => Math.floor(Math.random() * 9));
document.body.innerText = randoms;
#5
0
`const t = Array.from({length: n}, mapArrowFx);
`const t = Array.from({length:n},mapArrowFx);
1) const t10 = Array.from({length: 10}, (v, k) => k); [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1)const t10 = Array.from({length:10},(v,k)=> k); [0,1,2,3,4,5,6,7,8,9]
2) const tEven = Array.from({length: 10}, (v, k) => 2*k); [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
2)const tEven = Array.from({length:10},(v,k)=> 2 * k); [0,2,4,6,8,10,12,14,16,18]
........
........
3)
3)
let n=100; const tRandom= Array.from({length: n}, (v, k) => Math.floor(Math.random()*n));
设n = 100; const tRandom = Array.from({length:n},(v,k)=> Math.floor(Math.random()* n));
...
...
#1
17
What does Array#fill
do?
Array#fill做什么?
According to MDN
据MDN称
The
fill()
method fills all the elements of an array from a start index to an end index with a static value.fill()方法使用静态值将数组的所有元素从起始索引填充到结束索引。
You can use Function#apply
, Array#map
, Math.floor()
, Math.random()
.
您可以使用Function#apply,Array#map,Math.floor(),Math.random()。
In ES6, Array#from
and Arrow function can be used.
在ES6中,可以使用Array#from和Arrow函数。
Array.from({length: 6}, () => Math.floor(Math.random() * 9));
Array.apply(null, Array(6)).map(() => Math.floor(Math.random() * 9));
Array.apply(null,Array(6))。map(()=> Math.floor(Math.random()* 9));
var randomArr = Array.from({length: 6}, () => Math.floor(Math.random() * 9));
document.getElementById('result').innerHTML = JSON.stringify(randomArr, 0, 4); // For demo only
<pre id="result"></pre>
In ES5:
在ES5中:
Array.apply(null, Array(6)).map(function(item, index){
return Math.floor(Math.random() * 9);
});
var randomArr = Array.apply(null, Array(6)).map(function(item, index){
return Math.floor(Math.random() * 9)
});
document.getElementById('result').innerHTML = JSON.stringify(randomArr, 0, 4);
<pre id="result"></pre>
What is Array.apply(null, Array(n))
? Can new Array(n)
used here?
什么是Array.apply(null,Array(n))?可以使用新的Array(n)吗?
Both the above code create new array of six elements, each element having value as undefined
. However, when used new
syntax, the created array is not iterable. To make the array iterable, Array.apply(null, Array(6))
syntax is used.
上面的代码都创建了六个元素的新数组,每个元素的值都是undefined。但是,使用新语法时,创建的数组不可迭代。要使数组可迭代,使用Array.apply(null,Array(6))语法。
If you have lodash included on page, it's really easy.
如果你在页面上有lodash,那真的很容易。
_.times(6, _.random.bind(0, 100))
^ - Number of elements in array
^ - Random number range min
^^^ - Random number range max
Note: This answer is inspired from Colin Toh's blog
注意:这个答案的灵感来自Colin Toh的博客
#2
6
var randoms = Array(4).fill(Math.floor(Math.random() * 9));
This line of code will create a list of 4 of the same number because fill
takes a single value and repeats it for the length of the list. What you want to do is run the random number generator each time:
这行代码将创建一个由4个相同数字组成的列表,因为fill只接受一个值并在列表长度内重复它。你想要做的是每次运行随机数生成器:
var makeARandomNumber = function(){
return Math.floor(Math.random() * 9);
}
var randoms = Array(5).fill(0).map(makeARandomNumber);
console.log(randoms)
// => [4, 4, 3, 2, 6]
https://jsfiddle.net/t4jtjcde/
https://jsfiddle.net/t4jtjcde/
#3
3
Short and simple ES6 approach -
简短的ES6方法 -
// randomly generated n = 4 length array 0 <= array[n] <= 9
var randoms = Array.from({length: 4}, () => Math.floor(Math.random() * 10));
Enjoy!
请享用!
#4
1
I wonder if it's possible to get the right result with a one-liner...
我想知道是否有可能通过单线程获得正确的结果......
var randoms = [...Array(4)].map(虚 => Math.floor(Math.random() * 9));
document.body.innerText = randoms;
#5
0
`const t = Array.from({length: n}, mapArrowFx);
`const t = Array.from({length:n},mapArrowFx);
1) const t10 = Array.from({length: 10}, (v, k) => k); [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1)const t10 = Array.from({length:10},(v,k)=> k); [0,1,2,3,4,5,6,7,8,9]
2) const tEven = Array.from({length: 10}, (v, k) => 2*k); [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
2)const tEven = Array.from({length:10},(v,k)=> 2 * k); [0,2,4,6,8,10,12,14,16,18]
........
........
3)
3)
let n=100; const tRandom= Array.from({length: n}, (v, k) => Math.floor(Math.random()*n));
设n = 100; const tRandom = Array.from({length:n},(v,k)=> Math.floor(Math.random()* n));
...
...