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 random is always the same
Check the size of your collection
adornos
:The problem is that if the
size()
number of decorations is very small, for example 1, the only value that will be returned is 0 (1 is excluded).If, for example, there were 2 ornaments, it would return 0 and 1, and if you haven't done enough testing, it can make you believe that it always returns the same result.
Here is some information on how it works that might help you: random .nextInt(n)
From java 1.7 onwards you can use,
ThreadLocalRandom
belongs to the packagejava.util.concurrent.ThreadLocalRandom;
For versions prior to java 1.7
Extracted from this post
You
Random
don't seem to have any problem, possibly it's a coincidence that you always get the same one. Every time you call the methodgetBmp
a newRandom
:The only explanation is that the value of the measure of the decorations array is 1, for this reason a value is always obtained.
You can perform a test for example if you define that the value of ornaments.size() is 10 and print the value, you will get random numbers from 0 to 9: