Me gustaría saber cómo generar un número aleatorio entre dos valores dados.
Soy capaz de generar un número aleatorio con lo siguiente:
Random r = new Random();
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
a[i][j] = r.nextInt();
}
}
Sin embargo, ¿cómo puedo generar un número aleatorio entre 0 y 100 (inclusive)?
Puedes usar, por ejemplo, r.nextInt(101)
Para un "entre dos números" más genérico, utilice
Random r = new Random();
int low = 10;
int high = 100;
int result = r.nextInt(high-low) + low;
Esto le da un número aleatorio entre 10 (incluido) y 100 (excluido)
int Random = (int)(Math.random()*100);
si necesita generar más de un valor, entonces sólo tiene que utilizar el bucle for para ello
for (int i = 1; i <= 10 ; i++)
{
int Random = (int)(Math.random()*100);
System.out.println(Random);
}
Si desea especificar un rango más decente, como de 10 a 100 ( ambos están en el rango )
entonces el código sería :
int Random =10 + (int)(Math.random()*(91));
/* int Random = (min.value ) + (int)(Math.random()* ( Max - Min + 1));
*Where min is the smallest value You want to be the smallest number possible to
generate and Max is the biggest possible number to generate*/