Random DateTime value in C#
Recently I needed a function that return random date. I haven't found anything
that meet my needs. I've finally found the article
from Tawani's blog which presents rather long function. However, I like to keep
things simple, so I decided to write my own.
What are the requirements for the function:
- It has to be as short and simple as possible.
- It has to return random date from the given range [dateFrom, dateTo).
- The accuracy of one second is sufficient.
This is the function I've written:
The idea is simple: first it adds random day to the start date, and then adds random seconds to this. It uses `Math.Min` to protect from jumping out of the range while adding secound.