Many of the times we use Random Numbers in our code and we need these numbers in diffrent forms as:
Numbers should be in certain range i.e. From 20 to 50,
Numbers can be repeated or not etc.
Number Should be multiple of somthing or should not be multiple of somthing.
So becouse of these factors I have to write the same code repeatedly with some diff. conditions so i thought to make a universal random number genration code which can genrate a verity of random numbers. So first of all the code..
/***********************************************************************************************************************************
Action Script 2.0 class
@ Discription : Class for Genrating random numbers without repetation from a selected range
@ param : nTotal : Total random numbers to be genrated in number
@ param : nRangeMax : Largest number to be genrated random numbers to be genrated in number
@ param : nRangeMin : Smallest number to be genrated random numbers to be genrated if not provided considered "0" in number
@ param : bRepeat : Bolean value for Numbers to be genrated can be repeated or not (true if can be repeated) default TRUE
@ param : nMultipleOf : Number represent should be Multiple Of Number
@ param : bMultipleOf : Boolean if true no should be Multiple and false if Number should not be multiple of nMultipleOf
@ return : function genrate returns array of random Numbers
@ example to be written in fla
@**********************************************************************
@    import RandomNumber;
@    var nNumber:RandomNumber = new RandomNumber();
@    nA = nNumber.genrate(3,20,10,false,4,false);
@    //Above line means that you need 3 random number from 10 to 20 should be non repeated and should not be divisible of 4
@**********************************************************************
@ Last modified:    12/10/2005
@ Flash version:    7.0
@ Author        Ajay Pal Singh
@ Version        1.0.1
 **********************************************************************************************************************************/

class RandomNumber{
    private var nRangeMax:Number;
    private var nRangeMin:Number;
    private var nRange:Number;
    private var bRepeat:Boolean;
    private var n:Number;//n m & i are used as loop counters only
    private var m:Number;
    private var i:Number;
    private var nX:Number;
    private var aNx:Array = new Array();

    function RandomNumber(){
    }

    public function genrate(nTotal:Number,nRangeMaxEx:Number,nRangeMinEx:Number,bRepeatEx:Boolean,nMultipleOf:Number,bMultipleOf:Boolean){
        nRangeMin = (nRangeMinEx == undefined) ? 0:nRangeMinEx; //If undefined set to 0
        nMultipleOf = (nMultipleOf == undefined) ? 1:nMultipleOf; //If undefined set to 1
        bRepeat = (bRepeatEx == undefined) ? true:bRepeatEx; //If undefined set to true
        bMultipleOf = (bMultipleOf == undefined) ? true:bMultipleOf; //If undefined set to true
        nRangeMax = nRangeMaxEx;
        nRange = nRangeMax - nRangeMin;
        if(nRangeMax == undefined){
            trace("There is no range specified");
        }
        if(nTotal == undefined){
            trace("No Parameter is passed. Class need at least two parameters first for total Nos to be genrated and second for Maximum Range");
        }
        if(nRange < nTotal){
            trace("Range is less then total No's to be genrated");
        }else{
            for(n=0;n<nTotal;n++){
                nX = Math.round(Math.random()*nRange) + nRangeMin;
                aNx[n] = nX;
                if(!bRepeat){
                    for(m=0;m<n;m++){
                        if(aNx[n]==aNx[m]){
                            n--;
                        }
                    }
                }
                if(!bMultipleOf){
                    if((aNx[n]%nMultipleOf) == 0){
                        n--;
                    }
                }else{
                    if((aNx[n]%nMultipleOf) != 0){
                        n--;
                    }
                }
            }
            return aNx;
        }
    }

    public function clear(){
        nRange = (nRange == undefined) ? 0:nRange;
        for(i=0;i<nRange;i++){
            aNx.pop[nRange-i];
        }
    }
}