¿Qué método devuelve un int aleatorio entre un mínimo y máximo? ¿O no existe ese método?¿Cómo puedo generar un entero aleatorio entre min y max en java?
Lo que estoy buscando es algo como esto:
NAMEOFMETHOD (min, max)
(donde min y max son int
s)
que devuelve soemthing así:
8
(al azar)
Si tal método existe, ¿podría enlazar al th documentación relevante con su respuesta. Gracias.
Actualización: atempting para implementar la solución completa en la respuesta nextInt tengo esto:
class TestR
{
public static void main (String[]arg)
{
Random random = new Random() ;
int randomNumber = random.nextInt(5) + 2;
System.out.println (randomNumber) ;
}
}
sigo teniendo los mismos errores del compilador:
TestR.java:5: cannot find symbol
symbol : class Random
location: class TestR
Random random = new Random() ;
^
TestR.java:5: cannot find symbol
symbol : class Random
location: class TestR
Random random = new Random() ;
^
TestR.java:6: operator + cannot be applied to Random.nextInt,int
int randomNumber = random.nextInt(5) + 2;
^
TestR.java:6: incompatible types
found : <nulltype>
required: int
int randomNumber = random.nextInt(5) + 2;
^
4 errors
lo que va mal aquí ?
¿Ha olvidado para declarar 'random' y asignar una nueva instancia de' java.util.Random' a ella? – MAK
¿podría explicarme cómo hacerlo? – David
¿Declaraste sentencias de importación para java.util?Aleatorio – Shashi