I am currently working on a program that should generate random noise on a screen based on the 'coordinates' of a pixel. The coordinates should have the same color every time you restart the program. However, using the Java's util.Random, the results I get are not as random as I'd like:
I thought that if I used the combined coordinates (as in one integer formed from both coordinates next to eachother) each coordinate would have a different number. By using that number as a seed I expected to get a different random numbers for each coordinate to use for the rgb value of that coordinate.
This is the code I used:
public class Generate {
static Random Random;
public static int TileColor(int x, int y){
Random = new Random(Integer.valueOf(Integer.toString(x)+Integer.toString(y)));
int b = 1 + Random.nextInt(50);
int g = 1 + Random.nextInt(50);
int r = 1 + Random.nextInt(50);
int color = -Color.rgb888(r, g, b);
return color;
}
}
Is the pattern that the program creates due to the way java's Random function works or am I doing something wrong and should I try a different approach?
Update: I now tried to get rid of the problems surrounding concatenation by using the following code:
public static int TileColor(int x, int y){
Randomy = new Random(y);
Randomx = new Random(x);
Random = new Random(Integer.valueOf(Integer.toString(Randomx.nextInt(1234))+Integer.toString(Randomy.nextInt(1234))));
int b = 1 + Random.nextInt(100);
int g = 1 + Random.nextInt(100);
int r = 1 + Random.nextInt(100);
int color = -Color.rgb888(r, g, b);
return color;
}
Somehow, this also provided a (in my opinion) sufficiently random image:
This code does however reseed three times per pixel. Even though this is not a problem for me right now, I do consider changing this code in case I need better preformance later on.