1

Im trying to get a random number from the range of 0 to 2 (0,1,2), but I only want to get the number 2 times in a row and after that it needs to be another number. Like this:

rand = 1(1st time), rand = 1(2nd time), rand = 1(3rd time -> we go and pick some other number(0 or 2))

currently I figured out a way, but I have a bad feeling that it isnt the best solution to this problem.

So I have this:

private var previousNumber:int = -1;
    private var sameNumber:int = 0;
    private var positions:Array = [0,1,2];

    private function randomNumber(maxNumber:int, minNumber:int = 0, calledFromInside:Boolean = false):int
    {
        var rand:int;
        var numberToReturn:int;

        if(!calledFromInside) 
        {
            rand = Math.floor(Math.random() * (1 + maxNumber - minNumber) + minNumber);
            if( previousNumber == rand )
            {
                if( sameNumber < 2 )
                {
                    sameNumber++;
                    previousNumber = rand;
                    numberToReturn = positions[ rand ];
                }
                else
                {
                    positions.splice( rand, 1);
                    sameNumber = 0;
                    randomNumber(maxNumber, minNumber, true);
                }
            }
            else
            {
                previousNumber = rand;
                numberToReturn = positions[ rand ];
            }
        }
        else 
        {
            maxNumber--;
            rand = Math.floor(Math.random() * (1 + maxNumber - minNumber) + minNumber);
            numberToReturn = positions[ rand ];
            positions = [0,1,2];
        }
        return numberToReturn;
    }
6
  • Could you explain what do you need exactly ? And what do you mean by "2 times in a row" ?
    – akmozo
    Jan 26, 2015 at 0:44
  • 2 times in a row means, that i dont want to get (lets say) number Two, no more then 2 consecutive times, and when it attempts to get again number Two (for the 3rd time), then I want it to return another number, which is different from Two. Basically Ive done it but .... I think its not the right way to do this - I overkilled it.
    – GregorII
    Jan 26, 2015 at 1:28
  • So you need 6 randomized generations for 0,1 and 2 twice for every value ?
    – akmozo
    Jan 26, 2015 at 1:34
  • :D nooo, basically what Im using this is to create and place elements at a certain position on the screen. Imagine they are spawned always at the top edge of the screen and falling dow, now 0 means - topLeft corner of the screen, 1- in the topMiddle of the screen, and 2 - topRight corner of the screen. So I never want to spawn 3 items (consecutive) in one position. How many times they spawn doesnt matter, but lets say - infinite
    – GregorII
    Jan 26, 2015 at 1:47
  • But why you didn't tell us that from the beginning, it is easier to understand ;)
    – akmozo
    Jan 26, 2015 at 1:53

2 Answers 2

1

You just need to re-roll a random number each time you're encountering a third match.

private function randomNumber(maxNumber:int, minNumber:int = 0):int
{
    var rand:int= Math.floor(Math.random() * (1 + maxNumber - minNumber) + minNumber);
    if (rand==previousNumber) 
    {
        sameNumber++; // matches previous
        while (sameNumber>2) // more than two in a row - uh oh
        { // start rolling until it'll be a different number
            rand= Math.floor(Math.random() * (1 + maxNumber - minNumber) + minNumber);
            // reroll the number, just that, no recursion
            if (rand!=previousNumber) 
            { // we rolled a different number, clean up
                sameNumber=1;
                previousNumber=rand;
                return rand;
            }
        }
    } else {
        sameNumber=1;
        previousNumber=rand;
        return rand;
    }
}
1

To do what you want, you can use a counter to count the generations number for every value and then you can accept it or request another value.

In my example, I used a button just to show the generated random value :

// save the generations count of every values
var count:Object = {0: 0, 1: 0, 2: 0};

btn.addEventListener(
    MouseEvent.CLICK, 
    function(e:MouseEvent):void { 
        trace(get_random_value());
    }
)

function get_random_value():int 
{
    var good:Boolean = false;
    var value:int;

    while(!good){

        // generate our random value, this instruction is good only for values between 0 and 2 for other values :
        // value = Math.floor(Math.random() * (1 + max_value - min_value) + min_value);
        value = Math.floor(Math.random() * 3);

        // if we have got this value once, so take it
        if(count[value] < 2)
        {
            good = true;

            // count ++
            count[value] ++;

            // if there is another value which has reached the max successive generations number (2) so initialize its count number
            init_others(value);

        // otherwise, we got this value twice, so try to get another value
        } else {            
            good = false;
        }
    }
    return value;
}

// initialize counter of values if equals 2
function init_others(me:int):void 
{
    for(var val in count){
        if(val != me && count[val] == 2) count[val] = 0;
    }
}

Hope that can help.

2
  • I appreciate your answer but it is a bit hard for me to fallow, and I think the upper solution is simpler.
    – GregorII
    Jan 26, 2015 at 14:07
  • I don't know how it's hard this method ;) We count the number of every random value, if it's 2, then we have to get another one, that's all ! But the most important is that you have a solution for your problem.
    – akmozo
    Jan 26, 2015 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.