/* Generating random number sequences using the formula (linear congruence)
	x[k+1] = (a*x[k] + c)mod m
   where a, c, and m are parameters set by the user and passed as command line 
   parameters together with a seed i.e.x[0]
   As a simple example try  a=7, c=1, m=13, and seed=5
   A more sophisticated selection would be a=69069, c=0, 
   m=2^32=4294967296, and seed=31
   It will print out, in a sort of random order, up to m-1 distinct values. 
   Then it loops.
 */

public class Random 
{
    private static long seed = 13;
    private static long a;
    private static long c;
    private static long m;

    public static void random_init(long s) {
	if (s != 0) seed = s;
    }

    public static long random() {
	seed = (a*seed + c)%m;
	return seed;
    }

    public static void main(String[] args) {
	if (args.length != 4) {
	    System.out.println("usage: you must enter values for a, c, m, seed");
	    return;
	}
	a = Long.parseLong(args[0]);
	c = Long.parseLong(args[1]);
	m = Long.parseLong(args[2]);
	long s = Long.parseLong(args[3]);
	random_init(s);
	for (int k = 0; k < m-1; k++) { 
	    System.out.println(random());
	    if (k % 8 == 7) { // after 8 elements go to a new line
		System.out.println();
		try {
		    Thread.sleep(1000); // sleep for a second
		} catch (Exception e) {
		    System.out.println(e.getMessage());
	        }

	    }
	} 
    }
}
