I wrote a demo to test java.util.Random
and I want to produce a repeating list of the same 5 numbers, but I get the same value when set different seeds.In my program, seeds range from 0 to 4. As far as I know, different seeds produce different values and the same seed get the same value. So I think the result will be a repeating list of the same 5 numbers. But the actual values output are all the same. What's wrong with my code? Could anyone tell me?
import java.util.Random;
public class Main {
public Main() {
}
public static void main(String[] args) {
for (int i = 0 ; i <= 255; i++)
{
String hex = Integer.toHexString(randInt(0, 255, i % 5));
System.out.println(hex);
}
}
private static Random rand = new Random();
public static int randInt(int min, int max, long seed) {
rand.setSeed(seed);
System.out.println("seed:" + seed);
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}
The result is :
seed:0
bb
seed:1
bb
seed:2
bb
seed:3
bb
seed:4
bb
seed:0
bb
seed:1
bb
seed:2
bb
seed:3
bb
seed:4
bb
seed:0
bb
seed:1
bb
seed:2
bb
seed:3
bb
seed:4
bb
seed:0
bb
seed:1
...
...
...