Genera un número aleatorio de 90-100 repeticiones:
public class RandomTest {public static void main (string [] args) { / * * el método math.random () predeterminado al tipo doble, por lo que debe emitirse a int * / int x = (int) (math.random () * (100-90+1) +90); // (max-min+1)+min = min-max system.out.println (x); }}Genera 90-100 números aleatorios que no son de repetición:
import java.util.hashset; import java.util.random; import java.util.set; public class RandomTest {public static void main (string args []) {int max = 100; // valor máximo int min = 90; // valor mínimo int count = max-min; // Número aleatorio Random Random = new Random (); set <Integer> set = new Hashset <> (); // El contenedor hashset solo puede almacenar objetos no duplicados mientras (set.size () <count) {// número de elementos almacenados inhashset int x = random.nextint (max-min+1)+min; // crear un número aleatorio set.add (x); // Agregue un número aleatorio al contenedor hashset} para (int i: set) {// foreach recorriendo el elemento contenedor sistema.out.println (i);}}}}Se genera un número aleatorio de 90-100 repeticiones por segundo:
import java.util.random; import java.util.timer; import java.util.timerTask; public class RandomTest {Void Timer () {Timer Timer = new Timer (); // Crear el método Timer.schedule (new TimeTask () {public void run () {// TimeTask Ej. System.out.println (x);}}, 0,1000); // 0 significa que no hay retraso, 1000ms = 1s} public static void main (string [] args) {randomTest ran = new RandomTest (); ran.timer (); // llame a la tarea cronometrada}}Este artículo se reproduce en: https://www.idaobin.com/archives/301.html