Boost logo

Boost Users :

Subject: [Boost-users] [date_time] how to generate a random date?
From: Saygin Arkan (saygenius_at_[hidden])
Date: 2010-05-13 08:21:06


Hi,
Does boost's date provide a random date generation?
Or somehow should I generate my own date and then pass as a parameter?

date GeneratorUtils::getRandomDate() {
    int year, month ,day;
    day = 1 + rand() % 29;
    month = 1 + rand() % 11;
    year = 1980 + rand() % 25;
    date d(year, month, day);

    return d;
}

currently this is my implementation, but sometimes it returns invalid month
value.
terminate called after throwing an instance of
'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::gregorian::bad_day_of_month>
>'
  what(): Day of month is not valid for year

Probably because of something like 29th Feb or sth like that. I rarely saw
that exception.

I could modify the code easiy but I just wanted to know that if such a
functionality exists?
Thanks a lot, I've started to adore boost after uuid & date =)

-- 
Saygin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net