作者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/m.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