CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

[JAVA] Classe Random

newamilys
newamilys
Niveau 15
11 novembre 2013 à 11:04:15

Bonjour,

Je cherche à faire quelque chose de très simple en Java, mais je bloque sur le problème.

Je souhaite générer un nombre aléatoire entre minimum et maximum.
private Random randomGenerator; // En champs
randomGenerator = new Random(); // Dans le constructeur

// Méthode
public int nombreAleatoire (int min, int max) {
int index = randomGenerator.nextInt(max)
}

J'utilise index pour avoir un intervalle entre 0 et max-1. Le problème, c'est que j'aimerai entre min et max, et pour min, je galère à trouver comment faire.

newamilys
newamilys
Niveau 15
11 novembre 2013 à 11:09:16

Bon ben j'ai finalement trouvé, je ne sais pas si ça marche pour tout, mais ça a l'air ^^'

J'ai trouvé ça :

int index = randomGenerator.nextInt(max + 1 - min) + min;

[-Shana-]
[-Shana-]
Niveau 10
11 novembre 2013 à 12:13:25

J'utilisais ça moi quand je savais pas qu'il y avait une classe Random :hap: :

int lower = TRUC;
int higher = MACHIN;

int random = (int)(Math.random() * (higher-lower)) + lower;

Sous forums
  • Aide à l'achat Mac
  • Création de sites web
  • Création de Jeux
  • Linux
  • Programmation
  • Internet
  • Steam Deck
  • Macintosh
  • Hardware
La vidéo du moment