What is the best way to generate random dates?

Hey developers, the title pretty much says it all, I’ve been trying to make a random date generator (for example 3rd May 2073) but I’ve been running into issues regarding the best way to generate random dates, I’ve tried using math.random to generate a random number that can then be converted into a readable date with os.date, but the problem is if I make the maximum number in the random function too high, I end up with an invalid argument error. And so currently I’m very limited with dates that can be generated.

Instead of doing that, you can go much simpler by generating the day, month and year one by one.

local day = math.random(0, 32)
local month = math.random(0, 13)
local year = math.random(1500, 7000)

local generatedDate = day.."/"..month.."/"..year

print(generatedDate)

PS: I actually dont know if math.random generates between or it includes the numbers provided, so you should fix the code if there are problems

You can do it like this

math.random(1,99999)..math.random(1,99999)
1 Like

Thank you! After making some minor modifications (such as making it so it can’t generate February 31st for example) it works perfectly.

Your solution also works, so thank you!