jeudi 10 décembre 2020

Generate Random Number Using math.random, But Without Repeats

For a yearly Christmas event for an organization I'm a part of, we usually hold a raffle where people have the opportunity to win free prizes by just attending and getting a number ticket at the door. We use a program written in Flash (Using ActionScript 2.0) that selects a random number utilizing math.random, with some parameters attached to it, as shown below:

//maxNr = 999999999999999;


initRandom = function(){
    var nr = Math.ceil(Math.ceil(Math.random()*(maxNr));
    var nrString = "";
    for( var j=0; j<(maxNr.toString().length-nr.toString().length); j++){
        nrString += "0";
    }
    nrString += nr.toString();
    var holder = this.createEmptyMovieClip("holder",1);
    for( i=0; i<maxNr.toString().length; i++ ){
        var mc = holder.attachMovie("number","n"+i,i+10);
        mc._x = i*350;
        mc.anim_mc.gotoAndPlay( Math.floor(Math.random()*9) + 1 );
        this["iv"+i] = setInterval( this, "revealNumber", 2000 + (500*i), nrString.substr(i,1), i );
    }
    // scale (if needed) and center
    if( holder._width > Stage.width ){
        holder._width = Stage.width;
        holder._yscale = holder._xscale;
    }
    holder._x = Stage.width/2 - holder._width/2;
    holder._y = 100;
    // buttons
    back_btn.onRelease = function(){
        for(item in holder){
            holder[item].removeMovieClip();
        }
        gotoAndStop("intro");
    }
}

revealNumber = function( digit, i ){
    clearInterval( this["iv"+i] );
    holder["n"+i].gotoAndStop("done");
    holder["n"+i]["number_txt"].text = digit;
}

initRandom();

stop();

It's meant to return a random number between 1 and 1000, as defined by:

go_btn.onRelease = function(){
    maxNr = Math.max( 1000 , 1 );
    gotoAndStop("random");
}

stop();

It was written by a member of our organization who unfortunately passed away during the year, and I have little to no programming knowledge but I am a quick learner and have actually modified some of the code to get to the point it is currently. However, I'm trying to add in a parameter that would disallow the function from repeating a number, ie, excluding an already generated number from being reselected. I've spent days scouring any resource possible and have only met dead ends.

With the approach currently taken, is it possible to add in this parameter to this existing code, and how can I go about doing that?

Any help, suggestion or reply would be very greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire