how to generate random number between 0 and 2^32-1 in java?
I write link this:
long[]num = new long[size + 1];
Random random = new Random();
for (int i = 1; i < size + 1; i++) {
num[i] = (long)random.nextInt()+(long)(1<<31);
System.out.println(num[i]);
}
but it print
-1161730240
-1387884711
-3808952878
-3048911995
-2135413666
i don’t know why..
Your problem is where you try to add an offset to avoid negative numbers.
interprets the 1 as an int, shifts it 31 bits which makes it the largest negative int, and then it it casted to a long (still negative).
You want
as your offset.