Random: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
|||
Zeile 23: | Zeile 23: | ||
<syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision"> | <syntaxhighlight lang="java" class="mw-collapsible mw-collapsed" id="mw-customcollapsible-myDivision"> | ||
@Test | @Test | ||
public void testRandom(){ | public void testRandom() { | ||
for(int i=0;i<10000;i++){ | |||
assertTrue(1 > Evaluator.getNumberResult("random()", exerciseVariableMap, fillInVariableMap)); | |||
assertTrue(0 <= Evaluator.getNumberResult("random()", exerciseVariableMap, fillInVariableMap)); | |||
} | |||
@Test | @Test | ||
public void testRandomStatistically(){ | public void testRandomStatistically() { | ||
HashMap<Double, Integer> statistics = new HashMap<>(); //<'Random Number', 'Occurrences'> | |||
for (int i = 0; i < 10000; i++){ | |||
double random = Evaluator.getNumberResult("random()", exerciseVariableMap, fillInVariableMap); | |||
if (statistics.containsKey(random)){ | |||
statistics.put(random, statistics.get(random) +1); | |||
}else{ | |||
statistics.put(random, 1); | |||
} | |||
} | |||
int max = 0; | |||
for (int value : statistics.values() { | |||
if(value > max){ max = value; } | |||
} | |||
assertTrue(2 >= max); //max 2 same random numbers out of 10 000 | |||
assertTrue(9990 < statistics.values().size()); //max 9 random numbers overlapping out of 10 000 | |||
} | } | ||
@Test(expected= | @Test(expected=FunctionInvalidNumberOfArgumentsException.class) | ||
public void testRandomWithOneArgument(){ | public void testRandomWithOneArgument() { | ||
Evaluator.getNumberResult("random(4)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected= | @Test(expected=FunctionInvalidNumberOfArgumentsException.class) | ||
public void testRandomWithTwoArguments(){ | public void testRandomWithTwoArguments() { | ||
Evaluator.getNumberResult("random(4, 5)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
@Test(expected= | @Test(expected=FunctionInvalidNumberOfArgumentsException.class) | ||
public void testRandomWithThreeArguments(){ | public void testRandomWithThreeArguments() { | ||
Evaluator.getNumberResult("random(2, 4, 0.1)", exerciseVariableMap, fillInVariableMap); | |||
fail(); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Hinweise === | === Hinweise === | ||
* liefert niemals den Wert 1 zurück | * liefert niemals den Wert 1 zurück | ||
[[Kategorie:Evaluatorfunktion]] | [[Kategorie:Evaluatorfunktion]] |
Version vom 19. September 2017, 07:38 Uhr
Beschreibung
Die Funktion random liefert eine Pseudozufallszahl in Form einer Gleitkommazahl im Bereich von 0 (inklusiv) bis 1 (exklusiv) zurück. Die Funktion benötigt keine Parameter.
Syntax
random()
Parameter
- keine
Return Value
- Gibt eine Zufallszahl (double) zurück
Beispiele
Standardfälle
random() --> 0.451236789 random() --> 0.969492934
Benutzung mit Variablen
- keine
JUnit Tests
[Anzeigen]
@Test
public void testRandom() {
for(int i=0;i<10000;i++){
assertTrue(1 > Evaluator.getNumberResult("random()", exerciseVariableMap, fillInVariableMap));
assertTrue(0 <= Evaluator.getNumberResult("random()", exerciseVariableMap, fillInVariableMap));
}
@Test
public void testRandomStatistically() {
HashMap<Double, Integer> statistics = new HashMap<>(); //<'Random Number', 'Occurrences'>
for (int i = 0; i < 10000; i++){
double random = Evaluator.getNumberResult("random()", exerciseVariableMap, fillInVariableMap);
if (statistics.containsKey(random)){
statistics.put(random, statistics.get(random) +1);
}else{
statistics.put(random, 1);
}
}
int max = 0;
for (int value : statistics.values() {
if(value > max){ max = value; }
}
assertTrue(2 >= max); //max 2 same random numbers out of 10 000
assertTrue(9990 < statistics.values().size()); //max 9 random numbers overlapping out of 10 000
}
@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testRandomWithOneArgument() {
Evaluator.getNumberResult("random(4)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testRandomWithTwoArguments() {
Evaluator.getNumberResult("random(4, 5)", exerciseVariableMap, fillInVariableMap);
fail();
}
@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testRandomWithThreeArguments() {
Evaluator.getNumberResult("random(2, 4, 0.1)", exerciseVariableMap, fillInVariableMap);
fail();
}
Hinweise
- liefert niemals den Wert 1 zurück