1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.corpusTest;
15
16 import java.util.Random;
17
18 import org.junit.jupiter.api.Assertions;
19 import org.junit.jupiter.api.BeforeEach;
20
21 import ch.qos.logback.classic.corpus.RandomUtil;
22 import org.junit.jupiter.api.Test;
23
24 public class RandomUtilTest {
25 long now = System.currentTimeMillis();
26
27 @BeforeEach
28 public void setup() {
29 System.out.println(RandomUtilTest.class.getName() + " now=" + now);
30 }
31
32 @Test
33 public void smoke() {
34
35 int EXPECTED_AVERAGE = 6;
36 int EXPECTED_STD_DEVIATION = 3;
37
38 System.out.println();
39 Random r = new Random(now);
40 int len = 3000;
41 int[] valArray = new int[len];
42 for (int i = 0; i < len; i++) {
43 valArray[i] = RandomUtil.gaussianAsPositiveInt(r, EXPECTED_AVERAGE, EXPECTED_STD_DEVIATION);
44 }
45 double avg = average(valArray);
46
47 Assertions.assertEquals(EXPECTED_AVERAGE, avg, 0.3);
48 }
49
50 public double average(int[] va) {
51 double avg = 0;
52 for (int i = 0; i < va.length; i++) {
53 avg = (avg * i + va[i]) / (i + 1);
54 }
55 return avg;
56 }
57
58 }