Skip to content

Instantly share code, notes, and snippets.

@TGITS
Last active February 15, 2022 14:17
Show Gist options
  • Save TGITS/caf144bad442426351d41bf79745c4d3 to your computer and use it in GitHub Desktop.
Save TGITS/caf144bad442426351d41bf79745c4d3 to your computer and use it in GitHub Desktop.
Script (à exécuter sous JShell) d'exemple de création d'un générateur de nombre aléatoire
//A exécuter sous JShell : /open RandomGenerationExampleScript.java
//Penser à configurer la console en UTF-8 avec chcp 65001 avant de lancer jshell
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
//Utilisation du générateur par défaut
var randomGeneratorDefault = RandomGenerator.getDefault();
System.out.println("Génération d'une valeur aléatoire booléenne : " + randomGeneratorDefault.nextBoolean());
System.out.println("Génération d'une valeur aléatoire entière de type 'int' : " + randomGeneratorDefault.nextInt());
System.out.println("Génération d'une valeur aléatoire entière de type 'long' : " + randomGeneratorDefault.nextLong());
System.out.println("Génération d'une valeur aléatoire réelle de type 'double' : " + randomGeneratorDefault.nextDouble());
System.out.println("Génération d'une flux de 5 valeurs entières : ");
randomGeneratorDefault.ints(5).forEach(System.out::println);
System.out.println("Génération d'une flux de 5 valeurs réelles : ");
randomGeneratorDefault.doubles(5).forEach(System.out::println);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment