创建长度为n的阵列,在JavaScript中的随机数 [英] Creating array of length n with random numbers in JavaScript
问题描述
在随访创建指定长度的数组,这个答案,我执行了下面得到相应的结果,但填充有随机数,代替零。
VAR偶合=阵列(4).fill伪(Math.floor(的Math.random()* 9));
那么,从数学上讲这是随机,没事的。但我想的随机性是阵列中可见不仅运行之间,当然。愚蠢的电脑...不要我说的做。做我想做的!
我可以遍历并把它放在我的随机(和不同)值。但我不知道,纯粹的好奇,如果可能的话用单行得到正确的结果,像上面,MATLAB风格。我需要调用的的eval(功能()...)的?我听说了很多坏话的评估 ...
请注意,上述产生code是这样的:
[7,7,7,7]结果
[3,3,3,3]
块引用>等。同时,我想是这样
[1,2,3,4]结果
[4,3,8,4]
块引用>解决方案的什么的
阵列#填充
办?的据MDN
的
填写()
方法填充的所有从开始索引到一个静态值的结束索引数组中的元素。
块引用>您可以使用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply\"相对=nofollow>
功能#适用
,的阵列#地图
,的Math.floor()
,的的Math.random()
。Array.from({长度:6},()=&GT; Math.floor(的Math.random()* 9));
<击>
Array.apply(NULL,阵列(6))图(()=&GT; Math.floor(的Math.random()* 9));
击>
\r\r\r\rVAR randomArr = Array.from({长度:6},()=&GT ; Math.floor(的Math.random()* 9));\r
\r
的document.getElementById(结果)的innerHTML = JSON.stringify(randomArr,0,4)。 //为了演示仅\r
&LT; pre ID =结果&GT;&LT; / pre&GT;
\r
在ES5:
Array.apply(NULL,阵列(6))。图(功能(项指数){
返回Math.floor(的Math.random()* 9);
});
\r\r\r\rVAR randomArr = Array.apply(NULL,阵列(6))。图(功能(项指数){\r
返回Math.floor(的Math.random()* 9)\r
});\r
\r
的document.getElementById(结果)的innerHTML = JSON.stringify(randomArr,0,4);\r
&LT; pre ID =结果&GT;&LT; / pre&GT;
\r
什么是
Array.apply(NULL,阵列(N))
?可以通过新阵列(N)
这里使用?以上两种code创建六大要素的新数组,每个具有价值元素作为
未定义
。但使用时新
语法,创建数组不是迭代。为了使阵列可迭代,Array.apply(NULL,阵列(6))
语法。如果您有 lodash 包含的页面上,它真的很容易。
_。次(6 _.random.bind(0,100))
^ - 在数组元素的数量
^ - 随机数范围最小
^^^ - 随机数范围最大注意:这个答案是从<一个灵感href=\"http://colintoh.com/blog/lodash-10-javascript-utility-functions-stop-rewriting#3)_create_an_array_of_n_size_and_populate_them_with_unique_values_of_the_same_$p$pfix\"相对=nofollow>科林桃李的博客
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...
Note that the above produces code like this:
[7, 7, 7, 7]
[3, 3, 3, 3]etc. while I want something like
[1, 2, 3, 4]
[4, 3, 8, 4]
解决方案What does
Array#fill
do?According to MDN
The
fill()
method fills all the elements of an array from a start index to an end index with a static value.You can use
Function#apply
,Array#map
,Math.floor()
,Math.random()
.In ES6,
Array#from
and Arrow function can be used.Array.from({length: 6}, () => 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:
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))
? Cannew Array(n)
used here?Both the above code create new array of six elements, each element having value as
undefined
. However, when usednew
syntax, the created array is not iterable. To make the array iterable,Array.apply(null, Array(6))
syntax is used.
If you have lodash included on page, it's really easy.
_.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
这篇关于创建长度为n的阵列,在JavaScript中的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!