/*
 *  Copyright 2014 Alexey Andreev.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.teavm.classlib.java.util;

import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.TMath;
import org.teavm.classlib.java.lang.TObject;
import org.teavm.jso.JSBody;

/**
 *
 * @author Alexey Andreev
 */
public class TRandom extends TObject implements TSerializable {
    public TRandom() {
    }

    public TRandom(@SuppressWarnings("unused") long seed) {
    }

    public void setSeed(@SuppressWarnings("unused") long seed) {
    }

    protected int next(int bits) {
        return (int) (random() * (1L << TMath.min(32, bits)));
    }

    public void nextBytes(byte[] bytes) {
        for (int i = 0; i < bytes.length; ++i) {
            bytes[i] = (byte) next(8);
        }
    }

    public int nextInt() {
        return next(32);
    }

    public int nextInt(int n) {
        return (int) (random() * n);
    }

    public long nextLong() {
        return ((long) nextInt() << 32) | nextInt();
    }

    public boolean nextBoolean() {
        return nextInt() % 2 == 0;
    }

    public float nextFloat() {
        return (float) random();
    }

    public double nextDouble() {
        return random();
    }

    @JSBody(script = "return Math.random();")
    private static native double random();
}

@@ -909,16 +909,205 @@
 izable %7B
+%0A%0A    /** A stored gaussian value for nextGaussian() */%0A    private double storedGaussian;%0A    %0A    /** Whether storedGuassian value is valid */%0A    private boolean haveStoredGaussian;%0A    
 %0A    pub
@@ -1951,24 +1951,880 @@
 dom();%0A    %7D
+%0A    %0A    /**%0A     * Generate a random number with Gaussian distribution:%0A     * centered around 0 with a standard deviation of 1.0.%0A     */%0A    public double nextGaussian() %7B%0A%0A        /*%0A         * This implementation uses the polar method to generate two gaussian%0A         * values at a time. One is returned, and the other is stored to be returned%0A         * next time.%0A         */%0A%0A        if (haveStoredGaussian) %7B%0A            haveStoredGaussian = false;%0A            return storedGaussian;%0A        %7D%0A%0A        double v1, v2, s;%0A        do %7B%0A            v1 = 2 * nextDouble() - 1;%0A            v2 = 2 * nextDouble() - 1;%0A            s = v1 * v1 + v2 * v2;%0A        %7D while (s %3E= 1 %7C%7C s == 0);%0A%0A        double m = StrictMath.sqrt(-2 * StrictMath.log(s) / s);%0A        storedGaussian = v2 * m;%0A        haveStoredGaussian = true;%0A%0A        return v1 * m;%0A    %7D
 %0A%0A    @JSBod

Add implementation of Random.nextGaussian().