package linearcongruentialgenerator;
//Imports
import java.io.*;
import java.util.*;
/**
*
* @author Jared
*/
public class LinearCongruentialGenerator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
String fileName;
int Z, I, M, Seed;
Scanner s = new Scanner(System.in);
System.out.println("Whay is the name of the file?");
fileName = s.nextLine();
BufferedReader inputFile = new BufferedReader(new FileReader(fileName));
String[] line;
while (inputFile.ready()) {
line = inputFile.readLine().split(" ");
Z = Integer.parseInt(line[0]);
I = Integer.parseInt(line[1]);
M = Integer.parseInt(line[2]);
Seed = Integer.parseInt(line[3]);
int period = period(Z, I, M, Seed);
System.out.println("Case #1: " + period);
/* Test
LinearCongruentialGenerator result = new LinearCongruentialGenerator(3,2,10,5);
int answer = result.getAnswer();
System.out.println(answer);
System.out.println(result.next());
System.out.println(result.next());
System.out.println(result.next());
System.out.println(result.next());
System.out.println(result.next());
System.out.println(result.next());
System.out.println(result.next());
int period = period(533, 250, 314322, 7052);
System.out.println("\n" + period);
*/
}
}
//Variables
int Z, I, M, Seed, N, answer;
/**
*
* @param z
* @param i
* @param m
* @param seed
*/
public LinearCongruentialGenerator(int z, int i, int m, int seed) {
//These three are the constants.
Z = z;
I = i;
M = m;
//This is the seed.
Seed = seed;
//The generator:
N = (Z * Seed + I) % M;
answer = N;
}
public int next() {
LinearCongruentialGenerator temp = new LinearCongruentialGenerator(Z, I, M, N);
this.N = temp.answer;
return temp.answer;
}
public static int period(int z, int i, int m, int seed) {
//Variables
int Z, I, M, Seed, N1, N2 = 0;
int period = 0;
Z = z;
I = i;
M = m;
Seed = seed;
LinearCongruentialGenerator temp = new LinearCongruentialGenerator(Z, I, M, Seed);
N1 = temp.answer;
while (N1 != N2) {
N2 = temp.next();
period++;
}
return period;
}
public int getAnswer() {
return answer;
}
}