mardi 15 décembre 2015

Check for repeated value in a Linear congruential generator (PRNG) Java

public class Pseudotilfaeldigetal {

public static int[] x = new int[1000];
public static int modulus = 86408;
public static int a = 43205;
public static int c = 12345;
public static int seed;

public static Scanner tastatur = new Scanner(System.in); // her navngives vores scanner til tastatur

public static void main(String[] args) {

    algoritme(modulus, seed, a, c);
}

public static void algoritme(int modulus, int seed, int a, int c) {
    System.out.println("Select your seed ");
    x[0] = tastatur.nextInt(); //reads the next input, and uses as seed

    for (int i = 1; i < x.length; i++) { //Main for loop that generates numbers
        x[i] = (a * x[i - 1] + c) % modulus; //the number generation using LCG method
        System.out.println(x[i]); // Prints the newly generated value
        for (int j = 1; j < x.length; j++) { // cycles through all int in x[] array to check
            if (x[i] == x[j]) { //if current number is already in array
                System.out.println("Value has been repeated"); //Prints value already used
                return; // program ends. 
            }
        }
    }
}}

So I'm creating a program that is supposed to use the linear congruential generator to create a sequence of numbers. And I want to compare the outputs to each other to check for repeats and then have the program stop. However, with my current code I end up with the code stopping after the first generated number "55672" I have a generator build in excel showing me all the outputs, and there is no repeats. What am I doing wrong? Is this even a possible way to compare them, as I basically end up asking if x[i]=x[i] because it loops all numbers including the i variable and thus end up returning with true.

Tldr; How do i make a it check for repeats?




Aucun commentaire:

Enregistrer un commentaire