2013-05-06 52 views
0

我想要生成一個隨機數並與另一個數進行比較,如果它們相同,我希望隨機數增加1,然後將其添加到舞臺上。但是如果它不同於我想讓它直接添加到舞臺上。但如果數字相同,它不能正常工作,它會通過radomize ++,但仍然會添加生成的初始數字。可以somebnody請幫助我如何解決這個問題?Number generation going wrong

function randomizedorder() 
    { 
     randomize = Math.floor(Math.random() * (choices.length)); 

     trace("the random number is" + randomize); 

    if (randomize == indexcount) { 
      randomize++; 
      trace ("it goes through this pahse" + randomize); 

     } 
     else { 
       addChild(choices [randomize]); 
     } 

    } 
+0

不知道什麼是錯的代碼在這裏看起來okayish。我認爲你應該考慮事先隨機/加擾列表。也許克隆數組,然後隨機選擇隨機交換n個點,然後使用克隆數組添加(和檢查)子項。 – shaunhusain 2013-05-06 18:37:56

回答

1

want the random number to increase by one and then add it to the stage

但你是一個增加它,因爲的addChild是else子句中不添加任何東西的階段。

function randomizedorder() 
{ 
    randomize = Math.floor(Math.random() * (choices.length)); 

    trace("the random number is" + randomize); 

    if (randomize == indexcount) { 
     randomize++; 
     randomize = randomize % choices.length; 
     trace ("it goes through this pahse" + randomize); 
    } 

    addChild(choices [randomize]); 

} 

還需要在做什麼決定,如果隨機化等於indexcount,也是在這種情況下,你不能用它來選擇水下爆炸等於choices.length-1

編輯:我加入了模運算符因此,如果隨機化去出界將回到0

+0

是的,這工作thx。但我也需要知道隨機數是否越界,因爲隨機數超過了選擇的長度,我該如何問,而不是增加一個減少一個?那麼這是一個選擇是可用的。因爲它現在確實發生了,如果我有一個在1的索引,並且仍然有0的可用選項,但是如果隨機數來到1,它將移動到2。我需要讓它回到0我知道我可以要求它檢查最後的其他人,但是我怎麼問它將它添加到舞臺上? – tailedmouse 2013-05-06 20:00:50

+0

編輯我的答案,你可以使用模數運算符來將'randomize'回到0,如果它超出界限。 – 2013-05-06 21:28:11

+0

oooo fancy ..非常感謝你爲我教授新的東西:D也解決了這個問題:p我從來不知道這種關係。 – tailedmouse 2013-05-06 21:50:01