vendredi 25 mars 2016

Why is the emulator less random than my device?

I created a small app (as a project, with no plans to release it). Part of the code generates a grid of random numbers and and displays a grid using the sprite from a list with said number. It behaved as expected, at least on my Kindle Fire (and another device I tried it on).

The emulator, however, is a different story. I think the emulated device was a generic Samsung (4, maybe). The same code, when run on the emulator, fills about half the grid with one sprite, and half with a different sprite.

The grid might look like this on the emulator:

11177
11177
11777
11777
11777

instead of this on real devices:

64251
26253
87635
38415
28167

The relevant part of my code (yes, I should move new Random() elsewhere):

for (int i = 0; i < GRID; i++) {
      for (int j = 0; j < GRID; j++) {
            paint.setColor(Color.parseColor("#FBB117"));
            Random rand=new Random();
            int num=rand.nextInt(8);
            canvas.drawBitmap(bmp,frames[num],calcGridSpot(i,j),
                    paint);

 //... Eventually closing braces:
      }
 }

I have had issues in the past, in Java, with Random deciding to be less random (I believe it might be due to optimizations).

Why is the emulator behaving less randomly? (And how do I fix it?)




Aucun commentaire:

Enregistrer un commentaire