lundi 30 mai 2016

Using a random long to generate random doubles

I'm implementing my own random number generator, and the algorithm I'm using naturally gives me a nextLong() method. However, using this core method, I need to implement the other standard methods, such as nextLong(long), nextInt(), nextDouble(), and so on, in terms of nextLong(). For example:

public long nextLong(long n) {
    long bits, val;
    do {
        bits = (nextLong() << 1) >>> 1;
        val = bits % n;
    } while (bits - val + n - 1 < 0L);
    return val;
}

public int nextInt() {
    return (int)nextLong();
}

How can I implement nextDouble?




Aucun commentaire:

Enregistrer un commentaire