0

Currently, I have three different sets of images that are produced randomly with a random integer string. str2 and str3 work fine but for some reason when I use rnd.nextInt(1), it only returns 0. Not 0 and 1, only 0. I sampled it hundreds of times and every time it is nothing but 0. Again, the other two strings return randomly as expected. If I can't get this fixed, though, my app will be very boring.

Did I do something wrong or is there a better way to return 0 and 1 randomly? Thanks to all that help.

String str1 = "img_" + rnd.nextInt(1);
String str2 = "img_" + rnd.nextInt(2);
String str3 = "img_" + rnd.nextInt(3);
if (levelCounter <= 10) {imgView1.setImageDrawable(getResources().getDrawable(getResourceID(str1, "drawable", getApplicationContext())));
                    imgView1.setScaleType(ImageView.ScaleType.CENTER);
                    imgView1.setVisibility(View.VISIBLE);
} else if (levelCounter <= 18) {imgView1.setImageDrawable(getResources().getDrawable(getResourceID(str2, "drawable", getApplicationContext())));
                    imgView1.setScaleType(ImageView.ScaleType.CENTER);
                    imgView1.setVisibility(View.VISIBLE);
} else if (levelCounter >= 19) {imgView1.setImageDrawable(getResources().getDrawable(getResourceID(str3, "drawable", getApplicationContext())));
                    imgView1.setScaleType(ImageView.ScaleType.CENTER);
                    imgView1.setVisibility(View.VISIBLE);
} else {
                    Log.e(LOGCAT, "Counter Error: Unknown Level");
}
Ben
  • 25
  • 4

1 Answers1

0

nextInt(n) returns a random number between 0 and n-1. If you want a random 0 or 1, use nextInt(2).

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74