[問題] 請問不重複亂數的邏輯思考

作者: FunkyBass (假放克)   2015-03-20 15:05:47
程式碼如下:
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
作者: mmis1000 (秋月戀楓)   2015-03-20 15:22:00
http://jsbin.com/liyaso先把要出現的數字選出來在洗牌阿
作者: swallowcc (guest)   2015-03-20 16:15:00
把值塞進array裡再shuffle...大概像這樣 http://goo.gl/6xayg
作者: FunkyBass (假放克)   2015-03-20 16:27:00
謝謝兩位大大,已解決,洗牌這事情比我原先想的好太多

Links booklink

Contact Us: admin [ a t ] ucptt.com