mercredi 27 avril 2016

Java Modulo Function Gives Zero

I am attempting to make my own random class. I do realize that many good ones already exist, however I want to make my own.

Here is the gist of the class:

public class Random {
    private int seed;
    private float count;
    public Random() {
    }
    public Random(int seed) {
        this.seed = seed;
    }
    public double getNextDouble() {
        count += 1;
        return (System.nanoTime() * Math.pow(count + seed, 2)) % 1;
    }
}

I would have thought that the function would return a pseudo random number greater than and equal to zero and less than one. However, the getNextDouble() function unexpectedly returns 0. I've tracked the problem down to the % 1 part of the function.

Why is this causing the number returned to equal zero, and how would I fix this?

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire