1
Aprender a programar desde cero / genererar numeros con Math.random entre dos valores o en rango determinado Java
« en: 24 de Septiembre 2019, 10:13 »
Hola, mi pregunta es: ¿cómo se genera un numero aleatorio entre -8 y 0 , cero no inclusive, y entre -10 y 3 , -10 no inclusive, y entre 2 y 3 , 2 no inclusive?
Yo tengo esto pero no me sale el numero aleatorio negativo, ¿eso como se hace?
//genero un aleatorio entre 3 y -10 para poder hacer el ejecicio
int rango;
//(int)Math.random()*(num_maximo-num_minimo)-min
b=(int)(Math.random()*(3-10)+10;
String resul;
//Entre -8 y 0, 0 no incluido
r=(b<0)?"-8 y 0":" -8 y 0";
System.out.println(r);
//Entre -10 y 3, -10 no incluidos
r=(b<-9 && b<3)?"-10 y 3":"-10 y 3";
System.out.println(r);
//Entre 2 y 3, 2 no incluido
r=(b<3)?"2 y 3":" 4 y 5";
System.out.println(r);
Yo tengo esto pero no me sale el numero aleatorio negativo, ¿eso como se hace?
//genero un aleatorio entre 3 y -10 para poder hacer el ejecicio
int rango;
//(int)Math.random()*(num_maximo-num_minimo)-min
b=(int)(Math.random()*(3-10)+10;
String resul;
//Entre -8 y 0, 0 no incluido
r=(b<0)?"-8 y 0":" -8 y 0";
System.out.println(r);
//Entre -10 y 3, -10 no incluidos
r=(b<-9 && b<3)?"-10 y 3":"-10 y 3";
System.out.println(r);
//Entre 2 y 3, 2 no incluido
r=(b<3)?"2 y 3":" 4 y 5";
System.out.println(r);