tziuka
tziuka

Reputation: 545

AS3 random algorithm

I need a suggestion. I want to have a function that returns random numbers from let say 1 to 100, with condition to not repeat the chosen number. It is something like chess table that will be filled with something random and not one thing over another thing... If someone can tell a suggestion I'll be very happy. Thanks.

Upvotes: 0

Views: 330

Answers (2)

Vadim Gordienko
Vadim Gordienko

Reputation: 11

also like that...

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    /**
     * ...
     * @author Vadym Gordiienko
     */
    public class Main extends Sprite 
    {

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            var startArray:Array = generateNumberArray(100);
            var randomArray:Array = randomArray(startArray);
            trace("startArray = " + startArray);
            trace("randomArray = " + randomArray);
        }
        /**
         * generate Array of numbers by length
         * @param   length
         * @return Array of numbers
         */
        public static function generateNumberArray(length:int):Array 
        {
            var numberArray:Array = [];
            for (var i:int = 0; i < length; i++) 
            {
                numberArray[i] = i+1;
            }

            return numberArray;
        }

        /**
         * generate randomly mixed array by input array
         * @param   inputArray - simple not mixed array
         * @return Array - mixed array
         */
        public static function randomArray(inputArray:Array):Array 
        {
            var randomArray:Array = [];
            var tempArray:Array = [];
            for (var i:int = 0; i < inputArray.length; i++) 
            {
                tempArray.push(inputArray[i]);
            }
            while (tempArray.length)
            {
                var randomNumber:int = Math.round(Math.random() * (tempArray.length - 1));// get random number of left array
                randomArray.push( tempArray[randomNumber] );
                tempArray.splice(randomNumber, 1); // remove randomed element from temporary aarray
            }
            tempArray = null;
            delete [tempArray];

            return randomArray;
        }

    }

}

Upvotes: 1

scunliffe
scunliffe

Reputation: 63588

Create an Array of 100 numbers (1..100), then 'sort' the Array by 'random'. You can then pull out the numbers one at a time working your way through the array.

I haven't tested the code below but I had these snippets available that you could piece together to achieve the intended result.

public static function randomNumber(min:Number, max:Number):Number{
    var rnd:Number = Math.floor((Math.random()*((max+1)-min))+min);
    return rnd;
}

public static function randomize(arr:Array):Array{
    var len:Number = arr.length;
    var rnd:Number;
    var tmp:Object;
    for(var i:Number=0;i<len;i++){
        rnd = randomNumber(0,(len-1));
        tmp = arr[i];
        arr[i] = arr[rnd];
        arr[rnd] = tmp;
    }
    return arr;
}

var setOfNumbers:Array = new Array();
for(var i:int=0;i<100;i++){
    setOfNumbers[i] = (i+1);
}
var shuffledSetOfNumbers:Array = randomize(setOfNumbers);

Notes:

  • For the purists this "randomizing" isn't "truly" random (if you're writing a Card shuffler for a Vegas gambling machine you'll want to use something different - case in point!)
  • My randomNumber and randomize functions above are static as I've typically included them that way in the apps I've needed them but you don't have to use it this way
  • My original lib used Number vs int or uint for some of the variables for more options when used but feel free to clean that up

Upvotes: 1

Related Questions