2016-03-25 6 views
1

Ich muss einen Wert basierend auf einer prozentualen Chance auswählen, dass dieser Wert ausgewählt wird. Zum Beispiel:Auswählen eines Werts basierend auf Prozentsätzen

  • 10% des Inkrementwerts, wenn ein
  • 20% der Inkrementwert Zeit b
  • 30% der Inkrementwert C Zeit
  • 40% der Zeit Inkrementwert d

Die Prozentsätze werden immer in den genau 100% bis

ich mehrere Lösungen wiegestoßen, aber festgestellt haben, dass sie möglicherweise nicht korrekt sein können. Hier ist ein Beispielprogramm der Lösung gebaut unter Verwendung erwähnt:

import java.util.Random; 

public class Main { 

    private static Random r = new Random(); 

    public static void main(String[] args) { 
     final int iterations = 1000000; 
     System.out.println("Testing percentage based random, " + iterations + " iterations"); 
     int onePercent = 0; 
     int sixPercent = 0; 
     int sevenPercent = 0; 
     int thirtySixPercent = 0; 
     int fiftyPercent = 0; 
     // Those values add up to 100% overall 
     for (int i = 0; i < iterations; i++) { 
      int random = r.nextInt(100); 
      if (random < 1) { 
       onePercent++; 
       continue; 
      } 
      if (random < 6) { 
       sixPercent++; 
       continue; 
      } 
      if (random < 7) { 
       sevenPercent++; 
       continue; 
      } 
      if (random < 36) { 
       thirtySixPercent++; 
       continue; 
      } 
      if (random < 50) { 
       fiftyPercent++; 
       continue; 
      } 
      // That can't be right because if random > 50 then nothing at all happens 
     } 
     System.out.println("One percent happened about " + (onePercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
     System.out.println("Six percent happened about " + (sixPercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
     System.out.println("Seven percent happened about " + (sevenPercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
     System.out.println("Thirty six percent happened about " + (thirtySixPercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
     System.out.println("Fifty percent happened about " + (fiftyPercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
    } 
} 

Ausgang:

Testing percentage based random, 1000000 iterations 
One percent happened about 0.99649996% of the time 
Six percent happened about 4.9925% of the time 
Seven percent happened about 1.0029999% of the time 
Thirty six percent happened about 29.001299% of the time 
Fifty percent happened about 14.0191% of the time 

Erwartete Ausgabe:

Testing percentage based random, 1000000 iterations 
One percent happened about 0.99649996% of the time 
Six percent happened about 6.9925% of the time 
Seven percent happened about 7.0029999% of the time 
Thirty six percent happened about 36.001299% of the time 
Fifty percent happened about 50.0191% of the time 

Ich glaube, ich brauche eine Art von Algorithmus zu verwenden, die konvertieren Prozentsätze in eine Skala von 0 bis 99, so dass der Zufallszahlengenerator einen Wert genau auswählen kann. Ich kann mir nicht vorstellen, wie ich das machen soll.

+0

Verwenden 'sonst if' anstelle der normalen' if' – Gaktan

+0

Gleiches Ergebnis verwendet, da ich nach der Erhöhung weiter, auch die Prozentsätze immer noch falsch sind – Quantum64

+0

Sie sind immer die Ergebnisse, sollten Sie erwarten. Sie erhalten nicht 50%, nur weil Sie auf Werte zwischen 36 und 50 testen. Was ist 14% Wahrscheinlichkeit. Das macht also Sinn. Was Sie tun sollten, ist, anstatt Werte unter 50 zu testen, sollten Sie Werte unter 36 + 50 testen.Natürlich funktioniert das für alle Ihre Fälle, nicht nur für diesen Fall. – Gaktan

Antwort

-1

Ich habe es herausgefunden. Sie müssen den bisher getesteten Prozentsatz im Auge behalten und ihn dem aktuellen Test hinzufügen.

import java.util.Random; 

public class Main { 

    private static Random r = new Random(); 

    public static void main(String[] args) { 
     final int iterations = 1000000; 
     System.out.println("Testing percentage based random, " + iterations + " iterations"); 
     int onePercent = 0; 
     int sixPercent = 0; 
     int sevenPercent = 0; 
     int thirtySixPercent = 0; 
     int fiftyPercent = 0; 
     // Those values add up to 100% overall 
     for (int i = 0; i < iterations; i++) { 
      int random = r.nextInt(100); 
      int totalPercent = 0; 
      if (random < totalPercent + 1) { 
       onePercent++; 
       continue; 
      } 
      totalPercent += 1; 
      if (random < totalPercent + 6) { 
       sixPercent++; 
       continue; 
      } 
      totalPercent += 6; 
      if (random < totalPercent + 7) { 
       sevenPercent++; 
       continue; 
      } 
      totalPercent += 7; 
      if (random < totalPercent + 36) { 
       thirtySixPercent++; 
       continue; 
      } 
      totalPercent += 36; 
      if (random < totalPercent + 50) { 
       fiftyPercent++; 
       continue; 
      } 
      totalPercent += 50; 
      // That can't be right because if random > 50 then nothing at all happens 
     } 
     System.out.println("One percent happened about " + (onePercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
     System.out.println("Six percent happened about " + (sixPercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
     System.out.println("Seven percent happened about " + (sevenPercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
     System.out.println("Thirty six percent happened about " + (thirtySixPercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
     System.out.println("Fifty percent happened about " + (fiftyPercent/Float.valueOf(iterations)) * 100 + "% of the time"); 
    } 
} 
+0

Übertreib es nicht, du musst nur den Fortfahren löschen; Aussage –

+0

Das funktioniert fast, außer ich brauche dort genau ein Ergebnis pro Iteration. Durch Entfernen der Continue-Anweisungen könnte die Summe der Prozentvariablen mehr oder weniger als die Gesamtzahl der Iterationen betragen. Ich denke, ich hätte das in der Frage angeben sollen. – Quantum64

1

Ihre Ergebnisse korrekt sind:

Fünfzig Prozent geschah etwa 14,0191% der Zeit

50 - 36 = 14

Sechsunddreißig Prozent etwa 29,001299% geschah der Zeit

über 36 - 7 = 29

Sieben Prozent geschah etwa 1,0029999% der Zeit

7 - 6 = 1

....

löschen alle 'continue'-Anweisungen, wenn Sie sie zusammenfassen möchten.

+0

Also kann ich etwas zu den Prozentwerten machen (ich kenne sie nicht zur Kompilierzeit), damit ich das erwartete Ergebnis bekomme (7 Prozent passiert ungefähr 7 Prozent) der ganzen Zeit)? – Quantum64

+0

Wenn Sie sie zur Kompilierzeit nicht kennen, welche Regel? Jeder%, jeder 5%? –