hello there, I have a bit of trouble figuring out how to finish a code for an assignment. I'm supposed to write a class that can generate a sequence of psudorandom integers using the linear congruence method. The trick of it is that the number generated will become the new seed and that's what I'm having trouble figuring out.

Here's my code.

public class pseudorandom {

	    private int multi;
	    private int seed;
	    private int incr;
	    private int modu;
	    
	    public pseudorandom (int m, int s, int i, int mod){
	    multi = m;
	    seed = s;
	    incr = i;
	    modu = mod;
	    }	
	    public void setMulti(int multi) {
			this.multi = multi;
		}

		public int getMulti() {
			return multi;
		}
	    	public void setSeed(int seed) {
			this.seed = seed;
		}
	    	public int getSeed() {
			return seed;
		}
	    	public void setIncr(int incr) {
			this.incr = incr;
		}

		public int getIncr() {
			return incr;
		}
	    	public void setModu(int modu) {
			this.modu = modu;
		}

		public int getModu() {
			return modu;
		}
		public int print(){
			return (((multi * seed) + incr) % modu);
		}

	    public static void main(String[] args) {
	    	pseudorandom x = new pseudorandom (3, 134, 23, 90);
	        
	    	System.out.println(x.print());
	    	
	    }



	}

Don't write this:

return (((multi * seed) + incr) % modu);

When you calculate the result store it into a variable. Then change the value of the seed with that variable and then return it.

ok I did that but now I gotta figure out how to make the pseudorandom code repeat, let the code produce a small table that looks like this with a range of pseudorandom double numbers in between 0.1 and 1
Range Numer of Occurences
[0.0..0.1) 99889
[0.1..0.2) 100309
etc.
as well as figure our how to make an applet for it.

Here's my Code

public class pseudorandom {

	    private int multi;
	    private int seed;
	    private int incr;
	    private int modu;
	    private int answer; 
	    private double dividemodu;
	    private int count; 
	    private int countremain;
	    
	    public pseudorandom (int m, int s, int i, int mod){
	    multi = m;
	    seed = s;
	    incr = i;
	    modu = mod;
	    }	
	    
	    



		public void setMulti(int multi) {
			this.multi = multi;
		}

		public int getMulti() {
			return multi;
		}
	    	public void setSeed(int seed) {
			this.seed = seed;
		}
	    	public int getSeed() {
			return seed;
		}
	    	public void setIncr(int incr) {
			this.incr = incr;
		}

		public int getIncr() {
			return incr;
		}
	    	public void setModu(int modu) {
			this.modu = modu;
		}

		public int getModu() {
			return modu;
		}
		
		public void setAnswer(int answer) {
			this.answer = answer;
				
		}
		public int getAnswer() {
			answer = (((multi * seed) + incr) % modu);
			setSeed(answer);
			return answer;
		}
		public double setDividemodu(double dividemodu) {
			this.dividemodu = dividemodu;
			return dividemodu;
		}


		public double getDividemodu() {
			return dividemodu;
		}


		public double dividemodu() {
			return setDividemodu((answer / modu));
		}


	    public void setCountremain(int countremain) {
			this.countremain = countremain;
		}


		public int getCountremain() {
			return countremain;
		}
		public void setCount(int count) {
			this.count = count;
		}


		public int getCount() {
			return count;
		}



		public static void main(String[] args) {
			
			pseudorandom x = new pseudorandom (3, 13, 23, 90);
	    	System.out.println(x.getAnswer());	
	    	System.out.println(x.dividemodu());
	    	pseudorandom y = new pseudorandom (3, x.answer, 23, 90);
	    	System.out.println(y.getAnswer());	
	    	System.out.println(y.dividemodu());



	    
	    	
	    }



	}
This article has been dead for over six months. Start a new discussion instead.