Skip to content

Instantly share code, notes, and snippets.

@qiuwch
Last active November 28, 2023 12:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qiuwch/41860c0e4c02ec2dd357 to your computer and use it in GitHub Desktop.
Save qiuwch/41860c0e4c02ec2dd357 to your computer and use it in GitHub Desktop.
Random permutation in C, equivalent to randperm in MATLAB, #tags: algorithm
int perm[SZ];
for (int i = 0; i < SZ; i++) perm[i] = i;
// Random permutation the order
for (int i = 0; i < SZ; i++) {
int j, t;
j = rand() % (SZ-i) + i;
t = perm[j]; perm[j] = perm[i]; perm[i] = t; // Swap i and j
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment