Génère un nombre aléatoire de 90-100 répétitions:
classe publique RandomTest {public static void main (String [] args) {/ * * La méthode math.random () par défaut est à double type, il doit donc être jeté à int * / int x = (int) (math.random () * (100-90 + 1) +90); // (max-min + 1) + min = min-max System.out.println (x); }}Génère 90-100 nombres aléatoires non répétés:
import java.util.hashset; import java.util.random; import java.util.set; public class randomTest {public static void main (String args []) {int max = 100; // valeur maximale int min = 90; // valeur minimale int count = max-min; // Numéro aléatoire Random Random = new Random (); set <Integer> set = new HashSet <> (); // Le conteneur HashSet ne peut stocker que des objets non duplicateurs tandis que (set.size () <count) {// Nombre d'éléments stockés inhashset int x = random.nextint (max-min + 1) + min; // Créer un nombre de nombres aléatoires.add (x); // Ajouter un nombre aléatoire dans le conteneur de hashset} pour (int i: set) {// foreach Traversal the Container Element System.out.println (i);}}}}Un nombre aléatoire de 90 à 100 répétitions est généré par seconde:
import java.util.random; import java.util.timer; import java.util.timertask; public class randomTest {void timer () {timer timer = new timer (); // Créer le Timer.Schedule (new Timertask () {public void run () {// TIMERTASK RUN Méthode pour implémenter l'interface runnable aléatoire aléatoire = new random (); int x = random.nextint (100-90 + 1) +90; // (max-min + 1) + min = min à max // int x = random.nextint (100)% (100-90 System.out.println (x);}}, 0,1000); // 0 signifie aucun retard, 1000ms = 1s} public static void main (String [] args) {randomTest ran = new randomTest (); ran.timer (); // appelle la tâche chronométrée}}Cet article est reproduit à: https://www.idaobin.com/archives/301.html