dimanche 31 mai 2015

Java: Random number in fixed range excluding specified number

In my (Java Android) game, I spawn coins at random positions.

Coins can appear at one of 6 positions horizontally across the screen (2 max per horizontal level).

What I do is create a random number between 0 and 5 for the first coin, then I want generate another random number excluding the position of the first coin.

So, for example, coin 1 is spawned at a random position between 0 and 5 - so let's say 4.

Then the next coin needs to be able to choose between 0-3 or 5. (basically 0-5 excluding 4).

I have managed to do it, but it's not very elegant and I'm sure there must be a better/cleaner way to achieve this, however, it escapes me.

The random(int number) method in the code below simply returns a random int from 0 to number-1 (uses nextInt) and randomBool() just returns a random boolean

Also, please bear in mind that I don't want to use any technique that keeps re-generating a random number if the one it produces is equal to the one we are trying to avoid.

code

    //Return a random number between 0 and 5 excluding the specified number
    private int getRandomExcluding(int excludedNumber){

        //If previous position was 0 then generate a number between 1 and 5
        if (excludedNumber==0){
                return random(5)+1;
        }
        //If position was 5, then generate and return number from 0-4
        else if (excludedNumber==5){
                return random(5);
        }

        //If number isn't 0 or 5 (then it is in range of 1-4 use a random bool to determine
        // if we are going to get a number less than or greater than the number we are excluding

        //True - get number lower than excluded number
        else if(randomBool()){

            //Excluded number is 1
            if (excludedNumber==1){
                return 0;  //Only posibility
            }

            //Excluded number is > 1
            else {
                //Return a random number between 0 (inclusive) and the excluded number (exclusive)
                return random(excludedNumber);
                }

        //False - get number higher than the excluded number (between exludedNumber+1 (inclusive) and 6(exlusive))
        else {
                return random(6-(excludedNumber+1))+(excludedNumber+1);
        }
    }




Aucun commentaire:

Enregistrer un commentaire