mercredi 31 mars 2021

How to implement a linear congruential generator in Java?

I am implementing my own PRNG where I should use the linear congruential generator. One of my methods should generate the next pseudo random number as seen below. The seed is retrieved from the user earlier.

MyRandom(long seed) {

Random random = new Random();
random.setSeed(seed);

}

This is the method that should generate the next pseudo random number. This is what I tried to write but I don't really know how I could make it work for the next coming numbers. a and b are the chosen constants and m is the chosen prime number.

int next(int bits) {

long seed;
seed = (a * seed) + (b % m);
return (int) (seed >>> (48 - bits));


}



Aucun commentaire:

Enregistrer un commentaire