Partage
  • Partager sur Facebook
  • Partager sur Twitter

[JAVA]NOMBRE enter 0 et 1001 en random ?

    29 septembre 2007 à 0:27:01

    Bonjour,

    J'aimerais qu'un chiffre soit choisi au bol entre 0 et 1001 mais un chiffre entier. Je pensais utiliser math.random et après multiplier la virable par mille puisque math.random est entre 0 et 1. Mais il y a toujours des nombre à virgule. Donc j'aimerais savoire s'il y a une possibilité tout bête pour enlever les nombre après la virgule.
    Merci d'avance !
    • Partager sur Facebook
    • Partager sur Twitter
      29 septembre 2007 à 0:57:21

      Salut,

      1. import java.util.Random;
      2. Random Aleatoire = new Random();
      3. int monEntier = Aleatoire.nextInt(1002);
      • Partager sur Facebook
      • Partager sur Twitter
        29 septembre 2007 à 0:59:01

        merci je le testerais demain ;)
        • Partager sur Facebook
        • Partager sur Twitter
          30 septembre 2007 à 1:34:40

          ou sinon, j'ai ça, qui te permet de choisir le minimum et le maximum :
          (pas moi qui ai créé cet classe)

          1. public class RandomInt {
          2.    public RandomInt(int l, int h) {
          3.            low = l;
          4.       high = h;
          5.    }
          6.    public int draw(){
          7.            int r = low + (int)((high - low + 1) * nextRandom());
          8.       if (r > high) r = high;
          9.       return r;
          10.    }
          11.    private static double nextRandom() {
          12.            int pos = (int)(java.lang.Math.random() * BUFFER_SIZE);
          13.       if (pos == BUFFER_SIZE) pos = BUFFER_SIZE - 1;
          14.       double r = buffer[pos];
          15.       buffer[pos] = java.lang.Math.random();
          16.       return r;
          17.    }
          18.    private static final int BUFFER_SIZE = 101;
          19.    private static double[] buffer = new double[BUFFER_SIZE];
          20.    static {
          21.            int i;
          22.       for (i = 0; i < BUFFER_SIZE; i++)
          23.          buffer[i] = java.lang.Math.random();
          24.    }
          25.    private int low;
          26.    private int high;
          27. }


          utilisation (aléatoire entre 15 et 600 (inclusif tout les deux)) :

          1. int monEntier = new RandomInt(15,600).draw();
          • Partager sur Facebook
          • Partager sur Twitter
          Altarapp.com - Applications, Code Snippets, API Wrappers et etc, le tout en C# le plus clair du temps!
            30 septembre 2007 à 6:47:55

            le tuto de m@téo est bien expliquer non?
            • Partager sur Facebook
            • Partager sur Twitter
              1 octobre 2007 à 1:04:29

              oui, certe, mais son tuto n'est pas en Java ^^
              • Partager sur Facebook
              • Partager sur Twitter
              Altarapp.com - Applications, Code Snippets, API Wrappers et etc, le tout en C# le plus clair du temps!
                1 octobre 2007 à 15:07:42

                Tu peux toujours utiliser la méthode Math.random() en faisant un transtypage.
                comme ceci :

                int nombreAleatoire;
                nombreAleatoire = (int) (Math.random() * 1000);
                • Partager sur Facebook
                • Partager sur Twitter
                Ubuntu 14.10

                [JAVA]NOMBRE enter 0 et 1001 en random ?

                × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                • Editeur
                • Markdown