Skip to content

Instantly share code, notes, and snippets.

@xytis
Last active December 24, 2015 13:29
Show Gist options
  • Save xytis/6805873 to your computer and use it in GitHub Desktop.
Save xytis/6805873 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#define NUMBER 1000000
int main(void) {
int *result;
result = (int *) malloc(NUMBER*sizeof(int));
srand(time(0));
int current = 0;
const int step = INT_MAX / NUMBER;
int limit = step;
int index = 0;
//Generate unique set:
for (; index < NUMBER; ++index) {
current += 1 + rand() % (limit-current);
limit += step;
result[index] = current;
}
//Shuffle the set:
for (index = 0; index < NUMBER; ++index) {
current = result[index];
limit = rand() % NUMBER;
result[index] = result[limit];
result[limit] = current;
}
//Print out the set:
int separator = 0;
for (index = 0; index < NUMBER; ++index) {
++separator;
if (separator == 6) {
printf("%11d\n", result[index]);
separator = 0;
} else {
printf("%11d ", result[index]);
}
}
free(result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment