In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

重複しないランダム配列

0から(入力された数-1)までの整数値をシャッフルします。

ソートを用いてランダム実数値が入った配列を並び替え、そのインデックス値を得ることによりランダムな並びになります。
処理速度はソートアルゴリズムのオーダーに依存します。

http://merl.ec.t.kanazawa-u.ac.jp/diary_public/index.php?butchi%2F2011%C7%AF9%B7%EE#i60c6bc6
Get Adobe Flash player
by butchi 24 Sep 2011
/**
 * Copyright butchi ( http://wonderfl.net/user/butchi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/w6Fk
 */

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    import flash.events.MouseEvent;
    import com.bit101.components.PushButton;
    
    public class randomArray extends Sprite {
        public function getRandomArray(length:uint):Array {
            var range_array:Array = new Array(length);
            for(var i_counter:int=0; i_counter<length; i_counter++) {
                range_array[i_counter] = Math.random();
            }
            var random_array:Array = range_array.sort(Array.NUMERIC | Array.RETURNINDEXEDARRAY);
            return(random_array);
        }
        public function randomArray() {
            var length_TextField:TextField = new TextField();
            length_TextField.type = TextFieldType.INPUT;
            length_TextField.restrict = "0-9";
            length_TextField.text = "10";
            length_TextField.border = true;
            length_TextField.background = true;
            length_TextField.width = 80;
            length_TextField.height = length_TextField.textHeight+4; // from http://labs.torques.jp/2009/09/25/560/
            addChild(length_TextField);

            var randomArray_TextField:TextField = new TextField();
            var randomArray_TextFormat:TextFormat = new TextFormat();
            randomArray_TextFormat.size = 30;
            randomArray_TextField.defaultTextFormat = randomArray_TextFormat;
            randomArray_TextField.setTextFormat(randomArray_TextFormat);
            randomArray_TextField.y = 20;
            randomArray_TextField.width = 450;
            randomArray_TextField.height = 400;
            randomArray_TextField.wordWrap = true;
            addChild(randomArray_TextField);
            update();
            
            var update_btn = new PushButton(this,90,0,"Update");
            update_btn.addEventListener(MouseEvent.CLICK, update);
            function update() {
                randomArray_TextField.text = getRandomArray(int(length_TextField.text)).toString();;
            }
        }
    }
}