如何获得没有重复的随机生成的数字

How to get random generated Numbers with no Repeat

大家好,我看过一些关于此的论坛,但似乎仍然无法弄清楚。

所以我有一个名为 aClockArray 的数组,这是一个自定义数组,其中包含 4 个影片剪辑。嵌套的影片剪辑的每一帧都有不同的颜色。我在构造函数中像这样设置了数组:

aClockArray = [playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8];

然后在另一个函数中,我有一个 for 循环设置来遍历数组中的所有对象,并让它们 gotoAndStop 在它们的嵌套影片剪辑中的随机帧上,它从 2-7 开始,我让它像这样随机化:

private function randomColorGenerator():void 
    {
        //Loop through wires and make them randomn generate color
        for (var i:int = 0; i < aClockArray.length; i++)
        {
           var currentWires = aClockArray[i];

           nWire = randomNumber(2, 7);

           currentWires.gotoAndStop(nWire);


        }
    }

现在这很完美,每次重新启动时我都会得到随机颜色。但我想要完成的是颜色不重复,所以 nWire = randomNumber(2, 7); 2-7 数字不重复。我将如何去做才能让这些数字随机生成而不重复?

这也是我的 randomNumber 函数:

//Generates a truly "random" number
    function randomNumber(low:Number=0, high:Number=1):Number
    {
      return Math.floor(Math.random() * (1+high-low)) + low;
    }

谢谢任何帮助!

试过类似的东西,但仍然有重复:(

//Loop through wires and make them randomn generate color
        for (var i:int = 0; i < aClockArray.length; i++)
        {
           var currentWires = aClockArray[i];
           var frames:Array = [2, 3, 4, 5, 6, 7, 8];
           var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1);


           currentWires.gotoAndStop(randomFrame);


        }

创建唯一可能结果的数组:

var frames:Array = [2, 3, 4, 5, 6, 7];

然后使用 splice:

从该数组的随机索引处删除
var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1);

我不会给你确切的代码,因为我不是用 actionscript 编写的。但是,你只需要保存你最后使用的颜色并避免它:

{next_color = randomNumber(2,7)} until next_color != last_color;
currentWires.gotoAndStop(next_color);
last_color = next_color;

中提琴!伪随机颜色。

我建议如下:

  1. 创建预定义值列表
  2. 随机化列表
  3. 在每次迭代中移动列表的第一个值

例如:

    import flash.display.MovieClip;

    function randomizeSort(obj1:Boolean, obj2:Object):int
    {
        var randNum:int = -1 + Math.floor((Math.random() * 3));
        return randNum;
    }

    var framesList:Vector.<int> = Vector.<int>([2, 3, 4, 5, 6, 7])
    framesList.sort(randomizeSort);
    trace(framesList);

    var tempColor:int;
    var clipsList:Vector.<MovieClip> = Vector.<MovieClip>([playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8]);
    var clipsCount:int = clipsList.length;
    for (var clipIndex:int = 0; clipIndex < clipsCount; clipIndex++)
    {
        tempColor = framesList.shift();
        trace(tempColor);
    }

这是制作原型的理想人选:

MovieClip.prototype.gotoRandomFrame = function():void
{
    if(!this.frameReferences)
    {
        this.frameReferences = [];
        for(var i:int = 0; i < this.totalFrames; i++)
        {
            this.frameReferences.push(i + 1);
        }       
    }
    var index:int = this.frameReferences.splice(Math.floor(Math.random() * this.frameReferences.length), 1);
    if(!this.frameReferences.length)
    {
        this.frameReferences = null;        
    }
    this.gotoAndStop(index);
}

感谢这个原型,您现在可以在任何动画剪辑上调用此方法并让它们显示时间轴的独特帧,直到它们全部显示完并重新开始。

mymovie.gotoRandomFrame();

其他答案是正确的,但没有考虑到多个动画片段的情况。必须创建与您拥有的动画片段一样多的代码和数组片段是很糟糕的。