[問題] 請問不重複亂數的邏輯思考
程式碼如下:
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://www.ptt.cc/bbs/Ajax/M.1426835149.A.5BA.html
→
03/20 15:22, , 1F
03/20 15:22, 1F
→
03/20 15:23, , 2F
03/20 15:23, 2F
推
03/20 16:15, , 3F
03/20 16:15, 3F
→
03/20 16:15, , 4F
03/20 16:15, 4F
→
03/20 16:27, , 5F
03/20 16:27, 5F
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章