Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How could a sequence of random dates be generated, given year interval?

What is needed here is a command that generates six dates given a range of years. For example:
12/10/1987
30/04/1998
22/02/2014
17/08/2017
19/07/2011
14/05/2004


How it could be done, with sed, gawk, etc?
by

1 Answer

Kajalsi45d
#!/usr/bin/gawk -f
BEGIN {
first=mktime(ARGV[1] " 01 01 00 00 00")
last=mktime(ARGV[2]+1 " 01 01 00 00 00")
if (ARGC == 4) { num=ARGV[3] } else { num=1 }
ARGC=1
range=last-first
srand(sprintf("%d%06d", systime(), PROCINFO["pid"]))
for (i=1; i <= num; i++) {
print strftime("%d/%m/%Y", range*rand()+first)
}
}

For example:
./randomdate.gawk 1987 2017 6
26/04/1992
28/04/2010
21/04/2005
17/02/2010
06/10/2016
04/04/1998

Login / Signup to Answer the Question.