JavaScript中一组数字的随机排列
可能重复:JavaScript中一组数字的随机排列,javascript,random,shuffle,Javascript,Random,Shuffle,可能重复: 我正在用JavaScript编写一个代码,其中我需要获取35个输入值,在数组中为每个值分配一个位置,然后将它们洗牌,以便它们以不同的顺序重新排列。因此: var sort = new Array(35); sort[0] = document.getElementById("d1p1").value; sort[1] = document.getElementById("d1p2").value; // ... // ... (till 35) var rand1 = Math.f
我正在用JavaScript编写一个代码,其中我需要获取35个输入值,在数组中为每个值分配一个位置,然后将它们洗牌,以便它们以不同的顺序重新排列。因此:
var sort = new Array(35);
sort[0] = document.getElementById("d1p1").value;
sort[1] = document.getElementById("d1p2").value;
// ...
// ... (till 35)
var rand1 = Math.floor(Math.random() * 35);
var rand2 = Math.floor(Math.random() * 35);
// ...
// ... (till 35)
var rsort = new Array(35);
rsort[rand1] = document.getElementById("d1p1").value;
rsort[rand2] = document.getElementById("d1p2").value;
唯一的问题是,由于Math.floor(Math.random()*35)多次从1-35生成一些相同的数字(好吧,我想这就是随机性的点),因此两个值有时被分配到相同的输入框,并且返回未定义的值。有什么想法吗?为了在随机排列中生成值的均匀分布,您应该这样做:
- 选择0到35之间的随机索引,并将第一个值与该索引交换
- 然后从1到35中选择另一个随机索引,并用该索引交换第二个值
- 继续对所有剩余指数(2-35)进行类似操作
// first make a copy of the original sort array
var rsort = new Array(sort.length);
for(var idx = 0; idx < sort.length; idx++)
{
rsort[idx] = sort[idx];
}
// then proceed to shuffle the rsort array
for(var idx = 0; idx < rsort.length; idx++)
{
var swpIdx = idx + Math.floor(Math.random() * (rsort.length - idx));
// now swap elements at idx and swpIdx
var tmp = rsort[idx];
rsort[idx] = rsort[swpIdx];
rsort[swpIdx] = tmp;
}
// here rsort[] will have been randomly shuffled (permuted)
//首先复制原始排序数组
var rsort=新数组(sort.length);
for(var idx=0;idx
我希望这会有所帮助。为了在随机排列中生成值的均匀分布,您应该这样做:
- 选择0到35之间的随机索引,并将第一个值与该索引交换
- 然后从1到35中选择另一个随机索引,并用该索引交换第二个值
- 继续对所有剩余指数(2-35)进行类似操作
// first make a copy of the original sort array
var rsort = new Array(sort.length);
for(var idx = 0; idx < sort.length; idx++)
{
rsort[idx] = sort[idx];
}
// then proceed to shuffle the rsort array
for(var idx = 0; idx < rsort.length; idx++)
{
var swpIdx = idx + Math.floor(Math.random() * (rsort.length - idx));
// now swap elements at idx and swpIdx
var tmp = rsort[idx];
rsort[idx] = rsort[swpIdx];
rsort[swpIdx] = tmp;
}
// here rsort[] will have been randomly shuffled (permuted)
//首先复制原始排序数组
var rsort=新数组(sort.length);
for(var idx=0;idx
我希望这会有所帮助。您可以使用此功能改编而成的小功能。另外,我会使用一个类,这样更容易获取所有输入
function randomArray(min, max) {
return (new Array(max-min))
.join(',').split(',')
.map(function(v,i){ return [Math.random(), min + i]; })
.sort().map(function(v) { return v[1]; });
}
var inputs = document.querySelectorAll('.myinput');
// Creates an array with all your input elements in random order
var randomInputs = randomArray(0, inputs.length).map(function(n){
return inputs[ n ];
});
演示:(按ctrl+enter键刷新)您可以使用此小功能。另外,我会使用一个类,这样更容易获取所有输入
function randomArray(min, max) {
return (new Array(max-min))
.join(',').split(',')
.map(function(v,i){ return [Math.random(), min + i]; })
.sort().map(function(v) { return v[1]; });
}
var inputs = document.querySelectorAll('.myinput');
// Creates an array with all your input elements in random order
var randomInputs = randomArray(0, inputs.length).map(function(n){
return inputs[ n ];
});
演示:(按ctrl+enter键刷新)制作副本就像var rsort=sort.slice()一样简单。Stefan的观点很好-剥猫皮的方法不止一种;)制作副本就像var rsort=sort.slice()一样简单。Stefan的观点很好——剥猫皮的方法不止一种;)这是一种不好的随机洗牌方式,它既低效(洗牌比排序更容易)又不正确(比较函数应该与元素的总顺序一致)。。。我不明白为什么这是一个不好的方式和不正确的。。它工作得很好,我以前在其他项目中使用过它,并完成了这项工作。@6502:在这里查看演示对不起,我在跟踪链接时弄糊涂了。这个算法很糟糕(就像我说的排序比洗牌更难),但如果我们假设
Math.random
(这个假设是Javascript中最合理的近似值)的精度是无限的,那么这个算法并不是不正确的。取消了否决票。但是请注意,“有效”并不意味着你有时看到它产生你认为正确的结果。@6502:我明白你的意思。随机性对眼睛来说是主观的,虽然“准确”的随机性很难实现,但这对大多数事情来说都很好;它“看起来”是随机的。我为制作了该脚本,它可以很好地创建“随机”平铺效果。这是随机洗牌的一种不好的方法,它既低效(洗牌比排序更容易),又不正确(比较函数应该与元素的总顺序一致)。嗯。。。我不明白为什么这是一个不好的方式和不正确的。。它工作得很好,我以前在其他项目中使用过它,并完成了这项工作。@6502:在这里查看演示对不起,我在跟踪链接时弄糊涂了。这个算法很糟糕(就像我说的排序比洗牌更难),但如果我们假设Math.random
(这个假设是Javascript中最合理的近似值)的精度是无限的,那么这个算法并不是不正确的。取消了否决票。但是请注意,“有效”并不意味着你有时看到它产生你认为正确的结果。@6502:我明白你的意思。随机性对眼睛来说是主观的,虽然“准确”的随机性很难实现,但这对大多数事情来说都很好;它“看起来”是随机的。我为制作了这个脚本,它可以很好地创建“随机”瓷砖效果。