hi, i alreday posted under http://forums.sun.com/thread.jspa?threadID=5355743&tstart=30

yesterday, but no one helped me. it's an assignment for university, i have only few hours left to complete it, any hints would really be great!!

here my post:

I have to write a frame with two componenents of squared size (i chose 450 x 450), each drawing 10,000 random points. after that the values are to be evaluated statistically...
one component has to use java.util.Random and the other one must be a self-implemented linear congruental generator. the java.util generator works fine, but let me post the 2 classes for my lcg, first the generator implementation:

public class LCD {
	
	private int seed;
	private int score;
    private int range;
    // variables needed for lcd
	int multiplier;
	int offset;
	
	public LCD(int seed, int range, int multipl, int offset){
		this.seed = seed;
		this.range = range;
		this.multiplier = multipl;
		this.offset = offset;
	}
 
	public int nextInt() {
		 
		score = (multiplier * seed + offset) % range;
		System.out.println(score);
		seed = score;
		return score;
	}
}

and the component class using the generator:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
 
 
public class LCDGenerator extends Component{
 
	private static final long serialVersionUID = 1L;
	
	int MAX = Integer.MAX_VALUE;
	
	//values calculated using the knuth theory
	LCD lcd = new LCD(127, MAX, 31, 7);
	LCD lcd1 = new LCD(327, MAX, 61, 13);
 
	
	public void paint(Graphics g){
		
		g.setColor(Color.RED);
		
		int x;
		int y;
		
		for(int i= 0; i <= 10000; i++){
 
			x = Math.abs(lcd.nextInt()/(MAX/450));
			y = Math.abs(lcd1.nextInt()/(MAX/450));
			
			g.drawLine(x, y, x, y);
		}
		
	}
}

here my issues:

the component works implemented like this, draws the points equelly distributed like the java.util.Random implementation, but i think it is no good solution. I don't want to (and my assignment actually does not allow me to) use 2 LCD objects in the component class, I'd like to have one class like

LCD lcd = new LCD(127, MAX, 31, 7);

and then in the paint() just sometthing like

x = Math.abs(lcd.nextInt()/(MAX/450));
y = Math.abs(lcd.nextInt()/(MAX/450));

but when i just use one lcd, it draws zickzack-lines, but i don't understand why, because this means, that my generator does not generate equally distributed values, right?

the other thing is the nasty normalization with Math.abs and /(MAX/450)); it is because i give Integer.MAX_VALUE as the range parameter. how can I make it better? I actually would like to give 450 as the range parameter, as it is the components width and height, but when I do it, only very few points are drawn, although I printed out the values and the period length is 449 as it should, but only with a high value like Integer.MAX_VALUE I get as much points as with the java.util.Random generator

I'd be happy to get help on both mentioned issues. if anyone wants me to post the java.util.Random generator code or the Main class, which sets up the frame, just tell me!

thanks a lot :)

Recommended Answers

All 2 Replies

As the previous poster says, I think the whole point of your assignment is to demonstrate that a self-made LCG with bad parameters doesn't work very well. As well as the abovementioned link, you may be interested in this article on my web site about the java.lang.Random algorithm and LCG generators.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.