Random: Unterschied zwischen den Versionen

Aus JACK Wiki
Wechseln zu: Navigation, Suche
(JUnit Tests)
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 >  getEvaluator().getNumberResult("random()"));
 
    assertTrue(0 <= getEvaluator().getNumberResult("random()"));
 
  }
 
}
 
 
 
 +
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'>
+
HashMap<Double, Integer> statistics = new HashMap<>();  //<'Random Number', 'Occurrences'>
  for (int i = 0; i < 10000; i++){
+
for (int i = 0; i < 10000; i++){
    double random = getEvaluator().getNumberResult("random()");
+
double random = Evaluator.getNumberResult("random()", exerciseVariableMap, fillInVariableMap);
    if (statistics.containsKey(random)){
+
if (statistics.containsKey(random)){
      statistics.put(random, statistics.get(random) +1);
+
statistics.put(random, statistics.get(random) +1);
    }else{
+
}else{
      statistics.put(random, 1);
+
statistics.put(random, 1);
    }
+
}
  }
+
}
 
+
 
  int max = 0;
+
int max = 0;
  for (int value : statistics.values()){
+
for (int value : statistics.values() {
    if(value > max){ max = value; }
+
if(value > max){ max = value; }
  }
+
}
 
+
  assertTrue(2 >= max); //max 2 same random numbers out of 10 000
+
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  
+
assertTrue(9990 < statistics.values().size()); //max 9 random numbers overlapping out of 10 000  
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testRandomWithOneArgument(){
+
public void testRandomWithOneArgument() {
  getEvaluator().getNumberResult("random(4)");
+
Evaluator.getNumberResult("random(4)", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testRandomWithTwoArguments(){
+
public void testRandomWithTwoArguments() {
  getEvaluator().getNumberResult("random(4, 5)");
+
Evaluator.getNumberResult("random(4, 5)", exerciseVariableMap, fillInVariableMap);
 +
fail();
 
}
 
}
+
 
@Test(expected=UnsupportedEvaluatorFunctionException.class)
+
@Test(expected=FunctionInvalidNumberOfArgumentsException.class)
public void testRandomWithThreeArguments(){
+
public void testRandomWithThreeArguments() {
  getEvaluator().getNumberResult("random(2, 4, 0.1)");
+
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