I need to generate an random array from a bigger array of int without repetition and in a range of numbers.
For e.g I have an array a[] with 1-200 int , What i need is random number array from all 200 int but of size 15.How could I implement this?
Fisher-Yates shuffle. In particular, shuffle just the first 15 elements and select them.
In particular, shuffle just the first 15 elements and select them. - sorry, but I didn't really get this part. Can you elaborate please?Random rnd = new Random();
int[] a = new int[200];
for (int i = 0; i < a.length; i++)
a[i] = i;
int[] r = new int[15];
for (int i = 0; i < r.length; i++) {
int j = rnd.nextInt(a.length - i);
r[i] = a[j];
a[j] = a[a.length - i - 1];
}
That should do it. Random grab something from a. Once you've grabbed it, replace it with the "last" value of a. The "last" value is easily derived from a.length and i.