问题描述
您好,
我试图了解随机函数在此函数中的作用。这是此函数的最初目的
此函数返回一个包含加扰值列表的数组。阵列中正好有四对数字(1,2,3和4)和一对未配对数字(5)。使用array.sort()方法以伪随机方式对数组进行排序,从而对数组中的值进行加扰。
Hello,
I am trying to understand what the random function does in this function. here is the original purpose of this function
This function returns an array that contains a scrambled list of values. There are exactly four pairs of numbers (1,2,3 and 4) and one unpaired number (5) in the array. The values in the array are scrambled by using the array.sort() method to sort the array in a pseudo random way.
function randomAnswers(){
var answers = [1,1,2,2,3,3,4,4,5];
answers.sort(function(item){
return .5 - Math.random();
})
return answers;
}
我的尝试:
我知道随机返回0(包括)到1(不包括)之间的数字。但我得不到.5 -Math.random();在这个函数中
What I have tried:
I know that random returns numbers between 0(included) to 1(not included). but I dont get .5 -Math.random(); in this function
推荐答案
=0 -- if the objects are "equal"
<0 -- if the first object is "less than" the second
>0 -- if the first object is "greater than" the second
排序然后可以根据他们的比较排列对象,
但是!在这里它接收-0.5和+0.5之间的随机值 - 从而导致不可预测的排列对象序列。
The Sort can then arrange the objects according to their comparisons,
BUT! Here it receives a "random" value between -0.5 and +0.5 -- thereby resulting in an unpredictably arranged sequence of objects.
这篇关于什么是随机回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!