Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate random number in specific range in Android? [duplicate]

People also ask

Can random numbers repeat?

The numbers generated are not truly random; typically, they form a sequence that repeats periodically, with a period so large that you can ignore it for ordinary purposes. The random number generator works by remembering a seed value which it uses to compute the next random number and also to compute a new seed.

How can I generate a random number within a range but exclude some?

int i = rand(1, 9); if i>=7 i++; return i; As long as you ensure that your mapping is 1:1, you can avoid skewing the randomness of your rand function. Ax. The other way round would be better: create numbers from1 to 8 and map 7 and 8 to 8 and 9.


Random r = new Random();
int i1 = r.nextInt(80 - 65) + 65;

This gives a random integer between 65 (inclusive) and 80 (exclusive), one of 65,66,...,78,79.


int min = 65;
int max = 80;

Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;

Note that nextInt(int max) returns an int between 0 inclusive and max exclusive. Hence the +1.