double nb;Exécution:
nb = Math.random();
System.out.println(nb);
0.9610715687122661Pour obtenir des nombres supérieurs à 1, multipliez le résultat par 10,100,1000...100000.
nb=nb*100;
96.10715687122661
Random random = new Random();Le constructeur Random crée un nouveau générateur de nombres. L'exécution de ce code retourne un chiffre entre 0 et 8 choisi par le générateur.
int nb;
nb = random.nextInt(9);
int genererInt(int borneInf, int borneSup){
Random random = new Random();
int nb;
nb = borneInf+random.nextInt(borneSup-borneInf);
return nb;
}
public static void main(String[] args) {
System.out.println("intervalle[10, 20]: "+generer(10,20));
}
intervalle[10, 20]: 15
byte[] genererBit(int taille){Exécution:
Random random = new Random();
byte[] tbyte = new byte[taille];
random.nextBytes(tbyte);
return tbyte;
}
public static void main(String[] args) {
byte[] tbyte = genererBit(20);
for(byte bit:tbyte)
System.out.print(" "+bit);
}
-8 16 -7 27 54 -44 -75 88 -91 -74 -124 10 -100 101 -96 -41 -101 71 33 74
boolean genererBool(){
Random random = new Random();
return random.nextBoolean();
}
float genererFloat(){Exécution:
Random random = new Random();
return random.nextFloat();
}
double genererDouble(){
Random random = new Random();
return random.nextDouble();
}
public static void main(String[] args) {
System.out.println("float: "+genererDouble());
System.out.println("double: "+genererFloat());
}
double: 0.8538674831662335En java, le double est stocké sur 8 octets et le float sur 4 octets.
float: 0.46791792
Please disable your ad blocker and refresh the window to use this website.