Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays AS3-防止从阵列中选择重复项_Arrays_Actionscript 3_Flash_Random_Duplicates - Fatal编程技术网

Arrays AS3-防止从阵列中选择重复项

Arrays AS3-防止从阵列中选择重复项,arrays,actionscript-3,flash,random,duplicates,Arrays,Actionscript 3,Flash,Random,Duplicates,我有一个由八个字符串组成的数组,我想把它以随机顺序放在舞台上(使用文本字段) 我可以毫无问题地选择任意8个字符串,使用Math.random在0-7之间选择一个数字,并将该项放在该索引的舞台上 但我正在努力防止添加重复项。有人有什么建议吗 多谢各位 var source:Array = ["one", "two", "three", "four", "five", "six", "seven", "eight"]; var displayedIndices:Array = []; var in

我有一个由八个字符串组成的数组,我想把它以随机顺序放在舞台上(使用文本字段)

我可以毫无问题地选择任意8个字符串,使用Math.random在0-7之间选择一个数字,并将该项放在该索引的舞台上

但我正在努力防止添加重复项。有人有什么建议吗

多谢各位

var source:Array = ["one", "two", "three", "four", "five", "six", "seven", "eight"];
var displayedIndices:Array = [];

var index:uint = Math.floor(Math.random())*source.length;
displayedIndices.push(index);
var newString:String = source[index];
addChildAt(myTextField(newString), index); //I suppose myTextField creates a textfield and returns it

//now you want to be sure not to add the same string again
//so you take a random index until it hasn't already been used
while (displayedIndices.indexOf(index) != -1)
{
   index = Math.floor(Math.random())*source.length;
}
//there your index has not already been treated
displayedIndices.push(index);
var newString:String = source[index];
addChildAt(myTextField(newString), index);

这段代码很有教育意义,您应该只使用它的第二部分。

洗牌数组,然后循环遍历它。以下是一些很好的例子:


每次运行math.random函数时,使用splice从数组中删除结果索引处的字符串。

这是一种方法:

var strings:Array = ["one", "two", "three", "four", "five", "six"];

// create a clone of your array that represents available strings  
var available:Array = strings.slice();

// choose/splice a random element from available until there are none remaining
while (available.length > 0)
{
   var choiceIndex:int = Math.random() * available.length;
   var choice:String = available[choiceIndex];
   available.splice(choiceIndex,1);
   trace (choice);
   // you could create a textfield here and assign choice to it
   // then add it to the display list
}

其概念是创建数组的克隆,然后从该数组中随机抽取1个元素,直到没有剩余元素为止。这样可以确保您永远不会有副本。

谢谢。我从一开始就尝试了类似的方法——使用第二个数组存储已经添加的字符串,然后如果下一个字符串已经包含在该数组中,则生成一个新的随机数。