package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
public class Pruebas extends Sprite
{
public var arr:Array = new Array("valor0","valor1","valor2","valor3","valor4","valor5")
public var str1:String=arr[0];
public var str2:String=arr[1];
public var str3:String=arr[2];
public var te1:TextField = new TextField();
public var te2 :TextField = new TextField();
public var te3 :TextField = new TextField();
public var btn :MovieClip = new MovieClip();
public function Pruebas()
{
te1.text = str1;
te2.text = str2;
te3.text = str3;
addChild(te1);
addChild(te2);
addChild(te3);
te2.x = 50;te3.x=100;
btn.graphics.beginFill(0xff00ff);
btn.graphics.drawCircle(151,150,15);
addChild(btn);
btn.addEventListener(MouseEvent.CLICK,cambiar);
}
public function cambiar(e:Event):void{
var minLimit:uint = 0;
var maxLimit:uint = arr.length-1;
var range:uint = maxLimit - minLimit;
var someNum:Number = Math.ceil(Math.random()*range) + minLimit;
if(str1 == arr[someNum]){
while(str1 == arr[someNum]){
someNum = Math.ceil(Math.random()*range) + minLimit;
}
str1 = arr[someNum];
}else{
str1 = arr[someNum];
}
someNum= Math.ceil(Math.random()*range) + minLimit;
if(str2 == arr[someNum]){
while(str2 == arr[someNum]){
someNum = Math.ceil(Math.random()*range) + minLimit;
}
str2 = arr[someNum];
}else{
str2 = arr[someNum];
}
someNum= Math.ceil(Math.random()*range) + minLimit;
if(str3 == arr[someNum]){
while(str3 == arr[someNum] || str3 ==str1 || str3 == str2){
someNum = Math.ceil(Math.random()*range) + minLimit;
}
str3 = arr[someNum];
}else{
str3 = arr[someNum];
}
te1.text = str1;
te2.text = str2;
te3.text = str3;
}
}
}
Eduardo检查这段代码我做了这个代码
if(imgactual == myXML.internas.item[someNum].@numero){
while(imgactual == myXML.internas.item[someNum].@numero){
someNum = Math.ceil(Math.random()*range) + minLimit;
}
imgactual = myXML.internas.item[someNum].@numero
}else{
imgactual = myXML.internas.item[someNum].@numero
}
someNum= Math.ceil(Math.random()*range) + minLimit;
if((imgpostactual == myXML.internas.item[someNum].@numero) || (imgpostactual == imgactual) ){
while(imgpostactual == myXML.internas.item[someNum].@numero || imgpostactual == imgactual){
someNum = Math.ceil(Math.random()*range) + minLimit;
}
imgpostactual = myXML.internas.item[someNum].@numero
}else{
imgpostactual = myXML.internas.item[someNum].@numero
}
someNum= Math.ceil(Math.random()*range) + minLimit;
if((imgpreactual == myXML.internas.item[someNum].@numero) || (imgpostactual == imgpreactual) || (imgpreactual == imgactual)){
while((imgpreactual == myXML.internas.item[someNum].@numero) || (imgpostactual == imgpreactual) || (imgpreactual == imgactual)){
someNum = Math.ceil(Math.random()*range) + minLimit;
}
imgpreactual = myXML.internas.item[someNum].@numero;
}else{
imgpreactual = myXML.internas.item[someNum].@numero;
}
trace(imgactual);
trace(imgpreactual);
trace(imgpostactual);
这段代码的不好之处在于,总是需要很长时间的循环:S我认为它不起作用:S
答案 0 :(得分:1)
基本上,你想在0和maxLimit之间选择3个不同的随机数,然后在那些索引位置选择数组的元素。一种方法是为每个索引提供相同的机会,就是创建一个包含子索引的数组,然后选择前三个元素。像这样:
// Create the array with the indexes
var idxArray:Array = new Array();
for(var i:int = 0; i<=maxLimit; i++) idxArray.push(i);
// Shuffle it
for(var j:int = 0; j<=someNumberBigEnough; j++){
var idx1:int = Math.ceil(Math.random()*range) + minLimit;
var idx2:int = Math.ceil(Math.random()*range) + minLimit;
// to shuffle, swap elements
var aux:int = idxArray[idx1];
idxArray[idx1] = idxArray[idx2];
idxArray[idx2] = aux;
}
// Pick the first thee
str1 = arr[idxArr[0]]
str2 = arr[idxArr[1]]
str3 = arr[idxArr[2]]
例如,someNumberBigEnough
可以等于range
答案 1 :(得分:0)
另一个解决方案是检查第二个选择是否与第一个选项是同一个对象,第三个选择是与第一个和第二个选择相同的对象,如果其中任何一个匹配,则抓取另一个随机对象。
类似的东西:
var index1:int = (int)(Math.random() * arr.length);
var index2:int = index1;
while (index2 == index1) {
index2 = (int)(Math.random() * arr.length);
}
var index3:int = index1;
while (index3 == index1 || index3 == index2) {
index3 = (int)(Math.random() * arr.length);
}
str1 = arr[index1];
str2 = arr[index2];
str3 = arr[index3];
do while
循环非常适合这种情况......但我无法记住语法。