#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static int cmp_int(const void *a, const void *b) {
int x = *(const int *)a;
int y = *(const int *)b;
return (x > y) - (x < y);
}
void generate_non_overlapping_random_ranges(int begin, int end, int num_ranges) {
int count = num_ranges * 2;
int *points = malloc(count * sizeof(int));
int used = 0;
if (!points) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
srand((unsigned)time(NULL));
// Generate unique random points, the random numbers is chaotic
while (used < count) {
int r = begin + rand() % (end - begin);
int duplicate = 0;
for (int i = 0; i < used; i++) {
if (points[i] == r) {
duplicate = 1;
break;
}
}
if (!duplicate)
points[used++] = r;
}
for (int i = 0; i < count; i += 2) {
printf("(%d, %d)\n", points[i], points[i + 1]);
}
// The ranges increase and become non‑overlapping only after sorting
qsort(points, count, sizeof(int), cmp_int);
// Once sorted, we pair them
for (int i = 0; i < count; i += 2) {
printf("(%d, %d)\n", points[i], points[i + 1]);
}
free(points);
}
int main(void) {
int start = 1;
int end = 500;
int num_ranges = 8;
generate_non_overlapping_random_ranges(start, end, num_ranges);
return 0;
}
/*
run:
(36, 49)
(59, 84)
(92, 149)
(160, 164)
(277, 297)
(304, 410)
(454, 455)
(466, 489)
*/