001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.corpusTest;
015
016import static org.junit.Assert.assertEquals;
017
018import java.util.Random;
019
020import org.junit.Before;
021import org.junit.Test;
022
023import ch.qos.logback.classic.corpus.RandomUtil;
024
025public class RandomUtilTest {
026    long now = System.currentTimeMillis();
027
028    @Before
029    public void setup() {
030        System.out.println(RandomUtilTest.class.getName() + " now=" + now);
031    }
032
033    @Test
034    public void smoke() {
035
036        int EXPECTED_AVERAGE = 6;
037        int EXPECTED_STD_DEVIATION = 3;
038
039        System.out.println();
040        Random r = new Random(now);
041        int len = 3000;
042        int[] valArray = new int[len];
043        for (int i = 0; i < len; i++) {
044            valArray[i] = RandomUtil.gaussianAsPositiveInt(r, EXPECTED_AVERAGE, EXPECTED_STD_DEVIATION);
045        }
046        double avg = average(valArray);
047
048        assertEquals(EXPECTED_AVERAGE, avg, 0.3);
049    }
050
051    public double average(int[] va) {
052        double avg = 0;
053        for (int i = 0; i < va.length; i++) {
054            avg = (avg * i + va[i]) / (i + 1);
055        }
056        return avg;
057    }
058
059}