Results 1 to 5 of 5

[JAVA] Verschiedene Zufallszahlen erzeugen

  1. #1 Reply With Quote
    Schwertmeister
    Join Date
    May 2009
    Location
    Österreich/ Kärnten
    Posts
    775
    Hallo.
    Ich habe ein Problem beim Zufallszahlen erzeugen. Ich will 20 verschiedene Zufallszahlen zwischen 0 und 20 erzeugen. Allerdings tauchen immer wieder doppelte Zahlen auf.
    Code:
     Random r = new Random(); 
    int ir = r.nextInt(20);
    Gibt es da irgendeine Funktion mit der man das machen kann oder muss ich selbst eine Methode schreiben?
    Meine Gothic II Modifikationen:
    Secret of Jumanga 1.0| Silvestermod 2011 | Secret of Jumanga 2.0
    Work in Progress:
    Zum Tode verurteilt
    Tools | Skripte für Modder:

    Sprachausgabe, problem? 1.3 | Pflanzenrespawn

    Umfi is offline

  2. #2 Reply With Quote

    Administrator
    Thoronador's Avatar
    Join Date
    Jul 2005
    Location
    Morrowind, Vvardenfell-Distrikt
    Posts
    13,859
    Es ist normal, dass bei Zufallszahlen irgendwann eine Zahl mehrfach vorkommt. Wenn du beispielsweise mit einem normalen, sechsseitigen Würfel würfelst, kann es dir doch auch passieren, dass bei sechs Würfen mehrmals die gleiche Zahl auftritt.

    Wenn du 20 verschiedene Zahlen aus dem Bereich von 0 bis 19 erzeugen willst, brauchst du übrigens kein Zufallselement mehr, da kannst du einfach die Zahlen nacheinander aufsteigend von 0 an aufschreiben.
    -Have you got anything without spam in it?-

    Almalexia sieht und hört gar nichts mehr.

    ~ Morrowind-Hilfe ~ Morrowind-Plugins ~ Neverwinter Nights-Hilfe ~
    ~ More then cleverness we need kindness. ~
    ~ Daylight ~
    Why did Newton not invent group theory? - Because he wasn't Abel.
    "After all, the mark of the truly great was not merely having power, but knowing how and when to use it."
    Thoronador is offline

  3. #3 Reply With Quote
    Schwertmeister
    Join Date
    May 2009
    Location
    Österreich/ Kärnten
    Posts
    775
    Ich erkläre mal was ich genau vorhabe, damit ihr mich versteht.

    Ich habe zwei Arrays.

    Code:
    double inhalt[]={0.01, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750, 1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000};
    double korb[] = new double[26];
    Jetzt will ich korb mit zufälligen inhalt füllen. Und das wollte ich so machen. Aber inhalt[] darf nur einmal an korb[] vergeben werden.

    Code:
    for (int i=0; i<26; i++)
      {
           korb[i] = inhalt[methoden.zufallszahl(1,25)];
           System.out.println("korb"+i+"= "+inhalt[methoden.zufallszahl(1,25)]);
      }
    Hier ist die Methode zur erzeugung der zz:

    Code:
      public static int zufallszahl(int von, int bis) //Zufallszahl zw von und bis
      {int zz= von + (int)((bis-von+1)*Math.random()); return zz;
      }
    Hoffe ihr versteht was ich meine.
    Meine Gothic II Modifikationen:
    Secret of Jumanga 1.0| Silvestermod 2011 | Secret of Jumanga 2.0
    Work in Progress:
    Zum Tode verurteilt
    Tools | Skripte für Modder:

    Sprachausgabe, problem? 1.3 | Pflanzenrespawn

    Umfi is offline

  4. #4 Reply With Quote
    Provinzheld Kiview's Avatar
    Join Date
    Oct 2007
    Location
    Gelsenkirchen
    Posts
    290
    Was du machen will ist Ziehen ohne Zurücklegen?

    Dann mach das so, nimm eine ArrayList, remove ein zufälliges Element und so weiter bis alle leer sind.

    Also sowas in der Art:

    Code:
    ArrayList<double> list = new ArrayList(inhalt) // weiß ich nichtmehr ob es den konstruktor gibt
    
    int i = 0;
    while (!list.isEmpty()) {
      korb[i] = list.get(zufallszahl(0, list.size());
      i++;
    }
    Mongol General: What is best in life?
    Conan: To crush your enemies, to see them driven before you, and to hear the lamentations of their women.
    Kiview is offline

  5. #5 Reply With Quote
    Schwertmeister
    Join Date
    May 2009
    Location
    Österreich/ Kärnten
    Posts
    775
    Okay danke.
    Habs jetzt geschafft.
    Meine Gothic II Modifikationen:
    Secret of Jumanga 1.0| Silvestermod 2011 | Secret of Jumanga 2.0
    Work in Progress:
    Zum Tode verurteilt
    Tools | Skripte für Modder:

    Sprachausgabe, problem? 1.3 | Pflanzenrespawn

    Umfi is offline

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •