samedi 2 février 2019

Why Android often generate the same random integers?

I use this function to generate random coordinates (x, y) on a plane:

public static int random_between(int from, int to){
    Random r = new Random();
    return from + r.nextInt(to-from+1);
}

int x = random_between(0, 255);
int y = random_between(0, 255);

I often get coordinates with the same x and y values, 'nicely' distributed on the diagonal line.

And it only happens with earlier versions of Android (<=6.0).

Why is this? I know it has something to do with the random number generation algorithm used, but I would like to understand the details.




Aucun commentaire:

Enregistrer un commentaire