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

看板Ajax作者 (假放克)時間10年前 (2015/03/20 15:05), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/1
程式碼如下: 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:23, , 2F
先把要出現的數字選出來在洗牌阿
03/20 15:23, 2F

03/20 16:15, , 3F
把值塞進array裡再shuffle...
03/20 16:15, 3F

03/20 16:15, , 4F
大概像這樣 http://goo.gl/6xayg
03/20 16:15, 4F

03/20 16:27, , 5F
謝謝兩位大大,已解決,洗牌這事情比我原先想的好太多
03/20 16:27, 5F
文章代碼(AID): #1L2yRDMw (Ajax)
文章代碼(AID): #1L2yRDMw (Ajax)