如何在彩票游戏中循环不同的数字?

时间:2014-03-11 00:38:37

标签: actionscript-3 random

public class Lottery extends Sprite {

    public var text0:TextField, text1:TextField, text2:TextField, text3:TextField, text4:TextField, text5:TextField;
    public var backImage:Sprite, luckyBtn:Sprite, clearBtn:Sprite;
    public var tfFormat:TextFormat = new TextFormat;

    public function Lottery()
    {
        setTextFieldFormat();
        loadGUI();
    }

    public function luckyDip(event:MouseEvent):void {
        for (var i:uint = 0; i <= 49; i++) {
            this["text" + i].text = Math.ceil(Math.random() * 49);
        }
    }

    public function resetFields(event:MouseEvent):void {
        for (var i:uint = 0; i <= 49; i++) {
            this["text" + i].text = "";
        }
    }

如何修改它以便我仍然可以生成随机序列,但不重复任何条目?

1 个答案:

答案 0 :(得分:0)

在彩票游戏中,您的结果数量有限,例如:1-50的5个数字。您所需要的只是创建1-N的随机唯一数字列表。

//How to use, one of the results: 9,40,44,29,4
trace(lotteryGenerator(5));

private function lotteryGenerator(results: uint, maxValue:uint = 50):Array{
    var i: uint, luckyNumber: uint, result: Array = [], added: uint;

    while(added < results){
        luckyNumber = 1 + Math.random() * (maxValue - 1);
        if(result.indexOf(luckyNumber) == -1){
            result.push(luckyNumber);
            added++;
        }
    }

    return result;
}