using System;
using System.Collections.Generic;
class Program
{
static List<(int Start, int End)> generateNonOverlappingRandomRanges(int begin, int end, int numRanges)
{
int count = numRanges * 2;
Random rnd = new Random();
// ------------------------------------------------------------
// Generate unique random points in [begin, end)
// The random numbers are chaotic and NOT sorted
// ------------------------------------------------------------
HashSet<int> uniquePoints = new HashSet<int>();
while (uniquePoints.Count < count) {
int r = begin + rnd.Next(end - begin);
uniquePoints.Add(r); // HashSet ensures uniqueness
}
// Move to a list so we can sort
List<int> points = new List<int>(uniquePoints);
// ------------------------------------------------------------
// Sort the points
// Only AFTER sorting do the ranges become increasing
// and automatically non‑overlapping
// ------------------------------------------------------------
points.Sort();
// ------------------------------------------------------------
// Pair adjacent sorted points into (start, end) ranges
// ------------------------------------------------------------
List<(int Start, int End)> ranges = new List<(int, int)>();
for (int i = 0; i < points.Count; i += 2) {
ranges.Add((points[i], points[i + 1]));
}
return ranges;
}
static void Main()
{
int start = 1;
int end = 500;
int numRanges = 8;
var ranges = generateNonOverlappingRandomRanges(start, end, numRanges);
foreach (var r in ranges) {
Console.WriteLine($"({r.Start}, {r.End})");
}
}
}
/*
run:
(19, 97)
(164, 207)
(236, 296)
(305, 358)
(368, 370)
(405, 431)
(440, 446)
(458, 485)
*/