In this post, I provide a simple code to create a random permutation in an array of length n.
public int[] getRandomPermutation(int n) {
int a[] = new int[n];
// Initialize the array
for (int i = 0; i < n; i++)
a[i] = i;
// Shuffle the array
for (int i = 0; i < n; i++) {
// Generate an index randomly
int index = (int)(Math.random() * n);
int temp = a[i];
a[i] = a[index];
a[index] = temp;
}
return a;
}
Create Random Permutation
In this post, I provide a simple code to create a random permutation in an array of length n.
Post Disclaimer
The information contained in this post is for general information purposes only. The information is provided by Create Random Permutation and while we endeavour to keep the information up to date and correct, we make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the post for any purpose.