0
Help with code please
How to organize code that displays random numbers in a range, but they should not be repeated?
4 Réponses
+ 1
get the numbers in an array
shuffle that array
iterate it
0
I'm new, can you show some sample code?
0
public class Program
{
public static void main(String[] args) {
Integer[] array = new Integer[]{1, 2, 3, 4};
Random rgen = new Random(); // Random number generator
for (int i=0; i<array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
for(int s: array)
System.out.println(s);
}
}