program RandomDates;
{$mode objfpc}{$H+}
uses
SysUtils, DateUtils, Math;
// Generate a random date between two years using TDateTime
function RandomDate(startYear, endYear: Integer): TDateTime;
var
startDate, endDate: TDateTime;
rangeDays: Integer;
offset: Integer;
begin
// Convert start and end years to timestamps
startDate := EncodeDate(startYear, 1, 1);
endDate := EncodeDate(endYear, 12, 31);
// Uniform distribution over the timestamp range
rangeDays := Trunc(endDate - startDate);
offset := Random(rangeDays + 1);
Result := startDate + offset;
end;
var
dates: array of TDateTime;
d: TDateTime;
i: Integer;
y, m, day: Word;
begin
Randomize; // Seed RNG
SetLength(dates, 10);
for i := 0 to High(dates) do
begin
dates[i] := RandomDate(1990, 2030);
end;
for d in dates do
begin
DecodeDate(d, y, m, day);
WriteLn(y, '-', m, '-', day);
end;
end.
(*
run:
1996-4-6
2023-9-21
2011-9-15
2005-4-18
2001-4-10
2002-3-2
2022-9-26
2011-10-24
2010-7-3
2029-3-25
*)