作者FunkyBass (假放克)
看板Ajax
标题[问题] 请问不重复乱数的逻辑思考
时间Fri Mar 20 15:05:47 2015
程式码如下:
var random = new Array();
random[0] = Math.floor(Math.random()*10);
random[1] = Math.floor(Math.random()*10);
random[2] = Math.floor(Math.random()*10);
random[3] = Math.floor(Math.random()*10);
random[4] = Math.floor(Math.random()*10);
random[5] = Math.floor(Math.random()*10);
random[6] = Math.floor(Math.random()*10);
random[7] = Math.floor(Math.random()*10);
random[8] = Math.floor(Math.random()*10);
random[9] = Math.floor(Math.random()*10);
for(var i = 0 ; i < 10 ; i++){
if(random[i] == random[i+1]){
random[i+1] = Math.floor(Math.random()*10);
i--;
continue;
}
}
我觉得外面好像还要在一层回圈,但是没有头绪,有前辈可以给我一些idea吗?
我现在目前的程式只能做到邻近的值是不相同的,但是没办法做到跳号的值是不同的
ex.temp[0]跟temp[2],总觉得逻辑思考有点乱。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 42.72.188.69
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Ajax/M.1426835149.A.5BA.html
2F:→ mmis1000: 先把要出现的数字选出来在洗牌阿 03/20 15:23
3F:推 swallowcc: 把值塞进array里再shuffle... 03/20 16:15
5F:→ FunkyBass: 谢谢两位大大,已解决,洗牌这事情比我原先想的好太多 03/20 16:27