How to generate non‑overlapping random ranges (start,end),(start,end) in Pascal

1 Answer

0 votes
program NonOverlappingRanges;

const
  MaxPoints = 100;  { enough for num_ranges * 2 }

type
  TIntArray = array[1..MaxPoints] of Integer;

{-------------------------------------------------------------}
{ Swap two integers                                            }
{-------------------------------------------------------------}
procedure Swap(var A, B: Integer);
var
  T: Integer;
begin
  T := A;
  A := B;
  B := T;
end;

{-------------------------------------------------------------}
{ Simple bubble sort (Turbo Pascal has no built‑in sort)       }
{-------------------------------------------------------------}
procedure Sort(var A: TIntArray; N: Integer);
var
  i, j: Integer;
begin
  for i := 1 to N - 1 do
    for j := 1 to N - i do
      if A[j] > A[j + 1] then
        Swap(A[j], A[j + 1]);
end;

{-------------------------------------------------------------}
{ Generate unique random points                                }
{ The random numbers are chaotic and NOT sorted                }
{ The ranges become increasing and non‑overlapping ONLY AFTER  }
{ we sort the points.                                          }
{-------------------------------------------------------------}
procedure GenerateNonOverlapping(beginVal, endVal, numRanges: Integer);
var
  Points: TIntArray;
  Count, Used, i, j, R: Integer;
  Duplicate: Boolean;
begin
  Count := numRanges * 2;
  Used := 0;

  Randomize;

  {-----------------------------------------------------------}
  { Generate Count UNIQUE random integers in [beginVal, endVal) }
  {-----------------------------------------------------------}
  while Used < Count do
  begin
    R := beginVal + Random(endVal - beginVal);  { chaotic random number }
    Duplicate := False;

    { check if already used }
    for i := 1 to Used do
      if Points[i] = R then
      begin
        Duplicate := True;
        Break;
      end;

    if not Duplicate then
    begin
      Inc(Used);
      Points[Used] := R;
    end;
  end;

  {-----------------------------------------------------------}
  { Sort the points                                            }
  { Only AFTER sorting do the ranges become increasing         }
  { and automatically non‑overlapping                          }
  {-----------------------------------------------------------}
  Sort(Points, Count);

  {-----------------------------------------------------------}
  { Pair adjacent sorted points into (start, end) ranges       }
  {-----------------------------------------------------------}
  for i := 1 to Count div 2 do
  begin
    j := (i - 1) * 2 + 1;
    WriteLn('(', Points[j], ', ', Points[j + 1], ')');
  end;
end;

{-------------------------------------------------------------}
{ Main program                                                 }
{-------------------------------------------------------------}
begin
  GenerateNonOverlapping(1, 500, 8);
end.



(*
run:

(57, 117)
(134, 155)
(232, 269)
(282, 314)
(319, 337)
(375, 385)
(395, 454)
(482, 484)

*)


 



answered Jan 27 by avibootz

Related questions

...