2011-03-17 18 views
0

我不知道如何使一個Math.random()函數,或類似的地方,它從0-9選擇一個數字。但隨着程序的進行,另一個隨機變量將出現。該隨機值是我想從0-9級別中刪除的值。 (另一個隨機變量也是從0-9)Flash AS3隨機函數,0-9,例外,例外

+0

你可以分享你迄今爲止嘗試過的例子嗎? – JeremyFromEarth 2011-03-17 21:46:06

回答

1

的Math.random()從0-1

function randomNum(modifier:int):Number{ 
    return Math.floor(Math.random()*(9-modifier)); 
} 

返回一個數編輯後的新代碼
傳遞要出抓住一個元件陣列的,它會返回隨機元素的值。 使用seedArray.splice(0,1);在這種情況下從陣列中移除元素將會是第一個元素。

var seedArray:Array = [10,1000,20,245,874687,57,3456]; 
trace(this.randomElement(seedArray)) 
seedArray.splice(0, 1); 
trace(this.randomElement(seedArray)) 
seedArray.splice(0, 1); 
trace(this.randomElement(seedArray)) 
seedArray.splice(0, 1); 
trace(this.randomElement(seedArray)) 
seedArray.splice(0, 1); 
trace(this.randomElement(seedArray)) 
seedArray.splice(0, 1); 
trace(this.randomElement(seedArray)) 
seedArray.splice(0, 1); 
trace(this.randomElement(seedArray)) 

    function randomElement(arr:Array):Number{ 
     var rand:int = Math.floor(Math.random()*arr.length) 
     return arr[rand]; 
    } 
+0

'Math.floor(Math.random()* 10)'then – www0z0k 2011-03-17 21:51:04

+0

as 1 * 10 = 10這不是他要求的 – 2011-03-17 21:52:51

+0

這是行不通的。我試過了。事情是...當我使用: function randomNum(modifier:int):Number { return Math.floor(Math.random()*(9-modifier)); } 我得到一個0-4之間的數字。但是我需要隨機函數從數組中挑選一個數字。每次按下按鈕時,我將刪除數組中的一個索引。所以如果我使用這個函數,我突然鎖定了5個索引,而不是我作爲修飾符的索引。 簡而言之:當我刪除一個索引時,我需要一個仍然可以選擇數組的隨機函數。 – Kasper 2011-03-17 21:59:29

0

這應該工作

private function zeroToNineExcept(toExclude:int):int{ 
    var retVal: int = -1; 
    if(toExclude < 0 || toExclude > 9) { 
     //trace a message, throw an argument error, ... 
     return retVal; 
    } 
    while(retVal = Math.round(Math.random() * 9)){ 
     if(retVal == toExclude){ 
      continue; 
     }else{ 
      return retVal; 
     } 
    } 
} 
+0

不確定。這是我的樣子: 數組的創建0-9。 var rand:int = Math.floor(Math.random()* 9); question.text = theArray [rand] 我想在每次按下按鈕之間做什麼,拼接數組的索引之一,並仍然使「蘭德」工作打印有問題的文本。 (我的數組保留文本) – Kasper 2011-03-17 22:19:16

+0

@Kasper - 所以你需要以隨機順序返回9個項目,每個只能返回一次& – www0z0k 2011-03-17 22:44:28

0

此功能會隨機獲得一個數組元素。然後將該元素從數組中拼出並返回元素的值。返回值和結果數據類型是唯一需要更改的內容。

private function getRandomArrayElement(arr:Array):string 
{ 
    var index:int = Math.floor(Math.random() * arr.length); 
    var result:string = arr[index]; 
    arr.splice(index, 1); 

    return result; 
} 

如果你想維護數組中的數據並返回一個隨機數組這將工作。

private function randomizeArray(arr:Array):Array 
{ 
    var result:Array = []; 

    while (arr.length > 0) 
    { 
     result.push(arr.splice(Math.floor(Math.random() * arr.length), 1)); 
    } 

    return result; 
} 

我的測試數據如下。

var testArray:Array = ["Hi", "Bye", "Okay", "Yes", "No", "Maybe", "Sometimes", "Anywhere", "You said it!"]; 
+0

所以這樣做,我不能從數組中獲得同樣的問題兩次? – Kasper 2011-03-18 08:38:08

+0

對於第二個你會使用一個額外的數組。someArray = randomizeArray(theArrayWithMyStrings)然後你可以在someArray上做所有的事情,因爲它現在是隨機的。您的原始數組不會混亂,數據以原始形式保存,以便日後可以使用。 (當你準備好再次隨機化,並開始整個過程​​)。我希望這是有道理的。 – Feltope 2011-03-19 06:07:12

+0

如果你不必保持原始數據的安全,你可以只做myArray = randomize(myArray),它只會混淆你的數組.......哦,不,它不會給你同樣的問題兩次。一旦你隨機化數組,你可以通過它們。 myArray [0] [1]等等,它們將以隨機順序排列。 – Feltope 2011-03-19 06:10:26

0

我認爲這是最好做的隨機前面:

var randomValues:Array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 
randomValues.sort(function(a:int, b:int):int {return Math.random() < 0.5 ? -1 : 1}); 

這個初始化後,randomValues.pop()將讓你從數組中未使用的價值。