Randomize

4

In my method I have a Random variable:

   public Bitmap getBmp(int nunidades,boolean base){
    Bitmap bmp = Bitmap.createBitmap(ladoWidth * nunidades, ladoHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    int nbase = 0;
    int nespacio = 0;
    if(base){
        if(baseinicial != null){
            canvas.drawBitmap(baseinicial,0,0,null);
            nbase = 1;
        }
        for(;nbase < nunidades;nbase++){
            canvas.drawBitmap(baserepeticion,nbase*ladoWidth,0,null);
        }
    }else {
        //adornos
        if(!type.equalsIgnoreCase("Columnas")){
            int random = rnd.nextInt(adornos.size());
            ObjetoAdorno obj = new ObjetoAdorno(adornos.get(random).getTiles());
            ObjetoAdorno objf = new ObjetoAdorno();
            objf.setAdornos(obj.getAdornos());
            int tile = obj.getnAdornos();
            Log.d(GLOBALES.TAG,"espacio="+nunidades);
            Log.d(GLOBALES.TAG,"random="+random);
            Log.d(GLOBALES.TAG,"tile="+tile);
            if(nunidades> tile){
                Log.d(GLOBALES.TAG,"hay espacio");
                for(;nespacio < nunidades;nespacio++){
                    baseadornos.add(baserepeticion);
                    if(tile > 1){
                        for(int i=0;i<tile;i++){
                            baseadornos.add(objf.getAdornos().get(i));
                        }
                    }else{
                        baseadornos.add(objf.getAdornos().get(0));
                    }
                }
            }else{
                Log.d(GLOBALES.TAG,"No hay espacio");
                for(;nespacio < nunidades;nespacio++){
                    baseadornos.add(baserepeticion);
                }
            }
        }else{
            for(;nbase < nunidades;nbase++){
                baseadornos.add(baserepeticion);
            }
        }
        for(int i=0;i < nunidades;i++){
            canvas.drawBitmap(baseadornos.get(i),i*ladoWidth,0,null);
        }
    }
    return bmp;
}

The problem is that the value of the random is always the same

    
asked by PiledroMurcia 23.05.2017 в 12:27
source

4 answers

3

Check the size of your collection adornos :

int random = rnd.nextInt(adornos.size());

The problem is that if the size() of ornaments is very small, for example 1, the only value that will return is 0 (1 is excluded).

If for example they were 2 ornaments, I would return 0 and 1, and if you have not done enough tests it can make you believe that it always gives you the same result.

I leave you information on how it works that may help you: random .nextInt (n)

    
answered by 23.05.2017 / 12:50
source
1

Your Random does not seem to have any problem, possibly it's a coincidence that you're always getting the same. Each time you call the getBmp method, a new Random will be generated:

Random rnd = new Random();
int max = 10; //Número máximo aleatorio que puede salir
int min = 1; //Número mínimo aleatorio que puede salir
int aleatorio = rnd.nextInt(max - min + 1) + min;
    
answered by 23.05.2017 в 12:34
1

From java 1.7 onwards you can use,

int randomNum = ThreadLocalRandom.current().nextInt(0, adornos.size()+ 1);

ThreadLocalRandom belongs to the package java.util.concurrent.ThreadLocalRandom;

For versions before java 1.7

int randomNum = new Random().nextInt((adornos.size() - 0) + 1) + 0;

Excerpted from this post

    
answered by 23.05.2017 в 12:32
0
  

The problem is that the value of the random is always the same

Random rnd = new Random();
int random = rnd.nextInt(adornos.size());

The only explanation is that the value of the measure of the ornaments array is 1, for this reason you always get a value.

You can perform a test for example if you define that the value of ornaments.size () is 10 and you print the value, you will get random numbers from 0 to 9:

Random rnd = new Random();
for(int i =0; i< 100;i++) {
    int random = rnd.nextInt(10);
    Log.i(TAG, "valor random: " +  random);
}
    
answered by 23.05.2017 в 17:19