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.
Verwenden 'sonst if' anstelle der normalen' if' – Gaktan
Gleiches Ergebnis verwendet, da ich nach der Erhöhung weiter, auch die Prozentsätze immer noch falsch sind – Quantum64
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