选择不重复的数组随机元素? [英] Select random elements from an array without repeats?

查看:211
本文介绍了选择不重复的数组随机元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我不能相信我没有赶上这更早。原来我的问题再次声明我的第一个变量,一遍又一遍,从根本上启动该程序新鲜的,而不是持续的。为了解决这个问题,我更换了前两行有这样的:

edit: I can't believe I didn't catch this sooner. Turns out my problem was re-declaring my first variables over and over again, essentially starting the program fresh instead of continuing it. To fix it, I replaced the first two lines with this:

if (initialized === undefined) {
    trace("INITIALIZING");
    var MCs = [];
    var lastPos = "intializer";
    var initialized = 1;
}

现在它就像一个魅力。我觉得自己像一个小白这一个;对不起任何人的时候,我浪费了。我会发布这个作为一个回答我自己的问题,但它不会让我,因为我还是新的。

Now it works like a charm. I feel like a noob for this one; sorry to anyone whose time I wasted. I'd post this as an answer to my own question, but it won't let me since I'm still new.

我试图做一个闪光,将随机选择一个广告,播放它,然后随机再踢。为此,我已经被洗牌的数组,然后的gotoAndPlay -ing标签的数组的第一个元素,然后删除该元素成功。在每个广告的结尾是的gotoAndPlay(1); 所有主要code是在第一帧。如果数组是空的,它将重建,并改组了。

I'm trying to make a flash that will randomly choose an ad, play it, and then randomly play another. To that end, I've succeeded by shuffling an array, and then gotoAndPlay-ing the label in the first element of the array, and then removing that element. At the end of each ad is gotoAndPlay(1); with all the main code being on the first frame. If the array is empty, it rebuilds it and reshuffles it.

现在的问题是,我不希望它通过所有这些重复的任何广告,直到其运行;我的认为的我已经得到了下来,但我并不积极。此外,我不想数组中的最后一个元素是与第一,在新的,所以在同一个广告永远不会在一行中显示两次。我想有它检测到,如果它只是使用的元素之一,它是关于使用匹配,并且洗牌如果出现这种情况,但在我的测试中它仍然在连续偶尔会表现出同样的广告的两倍。

The problem is, I don't want it to repeat any ads until its run through all of them; I think I've got that down, but I'm not positive. Further, I don't want the last element in the array to be the same as the first in the new one, so the same ad won't ever show twice in a row. I'm trying to have it detect if the element it just used matches the one it's about to use, and reshuffle if that happens, but in my testing it continues to occasionally show the same ad twice in a row.

我明明做错事,但作为全新的到的ActionScript3(事实上闪烁)我有很多的麻烦确定它是什么。这就是我现在所拥有的:

I'm obviously doing something wrong, but being entirely new to ActionScript3 (and in fact to flash) I'm having a lot of trouble identifying what it is. Here's what I have right now:

var MCs = [];
var lastPos = "intializer";

if (MCs.length == 0) {
    MCs = reset();
    if (lastPos == MCs[0]) {
        while (lastPos == MCs[0]) {
            MCs = reset();
        }
    }
}
if (MCs.length > 0) {
    lastPos = MCs[0];
    MCs.splice(0,1);
    gotoAndPlay(lastPos+"MC");
}

function reset(){
    var PrepMCs = new Array("Image1", "Image2", "Image3");
    var WorkMCs = new Array(PrepMCs.length);

    var randomPos:Number = 0;
    for (var i:int = 0; i < WorkMCs.length; i++)
    {
        randomPos = int(Math.random() * PrepMCs.length);
        WorkMCs[i] = PrepMCs.splice(randomPos, 1)[0];
    }
    return WorkMCs;
}

就个人而言,我宁愿只是做这个用JavaScript,HTML和图片;这将会是非常简单的。但对于托管/ CMS的原因,我没有任何控制权,我仅限于单个文件或code单块;我不能外部托管任何东西,这据我可以告诉叶闪存作为此我最好的选择。

Personally, I'd rather just do this with JavaScript, HTML, and images; it'd be really simple. But for hosting/CMS reasons I don't have any control over, I'm limited to a single file or a single block of code; I can't host anything externally, which as far as I can tell leaves Flash as my best option for this.

任何帮助将是很大的AP preciated,谢谢!如果我做了什么可怕的,可怕的错误,这是一个奇迹,这甚至可以运行在所有的,不要犹豫,告诉我!

Any help would be greatly appreciated, thanks! If I've done something horribly, horribly wrong, and it's a wonder this even runs at all, don't hesitate to tell me!

编辑:这只是发生在我,这是完全正常,如果第二次运行在同一顺序的第一次运行,等等。最主要的是,它需要是随机的。这可能是更容易实现。

edit: It just occurred to me, it is perfectly fine if the second run is in the same order as the first run, etc. The main thing is, it needs to be random. This is probably much easier to implement.

编辑2:海量DERP这里。它运行时,它都会重新初始化管委会 lastPos ...换句话说,它的每一个洗牌的时间和从头开始。我要研究的是如何只运行一行code。如果一个变量没有被初始化。

edit 2: MASSIVE DERP HERE. Every time it runs, it re-initializes MCs and lastPos... in other words, it's shuffling every time and starting over. What I should be researching is how to only run a line of code if a variable hasn't been initialized yet.

推荐答案

从@ 32bitKid大摇大摆偷,这是我的版本。

Blatantly stealing from @32bitKid, this is my version.

主要的问题,我与他的解决方案是推/拼接的想法。尽可能,我喜欢创造一次,并重新使用。收缩和增长的阵列粗大,就算有效。

The main problem I have with his solution is the push/splice idea. As much as possible, I like to create once, and reuse. Shrinking and growing arrays is bulky, even if effective.

此外,这种方法不重新排序阵列,其可以是或可以不是有价值

Also, this method does not re-order the array, which may or may not be valuable.

顺便说一句,我很喜欢他$ P $的方式pvents的previous项的重复(几乎为空)。

BTW, I like the way that he prevents a repeat of the previous item ("almost empty").

因此​​,这里是另一种方法:

So here is another method:

package
{

    public class RandomizedList
    {
        private var _items:Array;
        private var idxs:Array;
        private var rnd:int;
        private var priorItemIdx:int;
        private var curIdx:int;

        public function RandomizedList(inarr:Array)
        {
            items = inarr;
        }

        private function initRandomize():void
        {
            idxs = new Array();

            //Fisher-Yates initialization (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle):
            idxs[i] = 0;
            for (var i:int = 1; i < items.length; i++)
            {
                rnd = int(Math.random() * (i + 1));
                idxs[i] = idxs[rnd];
                idxs[rnd] = rnd;
            }

            curIdx = 0;
            priorItemIdx = -1;
        }

        private function randomize():void
        {
            var tempint:int;
            //Fisher-Yates (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle):
            for (var i:int = items.length; i >= 1; i--)
            {
                rnd = int(Math.random() * (i + 1));
                tempint = idxs[i];
                idxs[i] = idxs[rnd];
                idxs[rnd] = tempint;
            }

            curIdx = 0;
        }

        public function next():void
        {
            if (curIdx >= idxs.length)
            {
                randomize();
            }

            if (items.length > 1 && priorItemIdx == idxs[curIdx])
            {
                curIdx++;
            }

            priorItemIdx = idxs[curIdx++];
            return items[priorItemIdx];
        }

        public function get items():Array
        {
            return _items;
        }

        public function set items(value:Array):void
        {
            _items = value;
            initRandomize();
        }
    }
}

这篇关于选择不重复的数组随机元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆