How to create a list of random dates in C++

3 Answers

0 votes
#include <iostream>
#include <vector>
#include <random>
#include <chrono>

// Generate a single random valid calendar date using C++20 chrono types
std::chrono::year_month_day random_date(std::mt19937 &rng) {

    // Random year between 1990 and 2030 (inclusive)
    std::uniform_int_distribution<int> yearDist(1990, 2030);

    // Random month between 1 and 12
    std::uniform_int_distribution<unsigned> monthDist(1, 12);

    int year = yearDist(rng);
    unsigned month = monthDist(rng);

    // Number of days in each month (February handled separately)
    static const unsigned days_in_month[] =
        {31,28,31,30,31,30,31,31,30,31,30,31};

    // Start with the normal number of days for this month
    unsigned dayMax = days_in_month[month - 1];

    // If February, check whether this year is a leap year
    if (month == 2) {
        bool leap = std::chrono::year{year}.is_leap();
        if (leap) dayMax = 29;  // February has 29 days in leap years
    }

    // Random day between 1 and the correct number of days for this month
    std::uniform_int_distribution<unsigned> dayDist(1, dayMax);
    unsigned day = dayDist(rng);

    // Construct a strongly‑typed chrono date object
    return std::chrono::year_month_day{
        std::chrono::year{year},
        std::chrono::month{month},
        std::chrono::day{day}
    };
}

int main() {
    // Initialize a high‑quality random number generator
    std::mt19937 rng(std::random_device{}());

    // Store the generated dates
    std::vector<std::chrono::year_month_day> dates;

    // Generate 10 random dates
    for (int i = 0; i < 10; i++) {
        dates.push_back(random_date(rng));
    }

    // Print each date in YYYY-MM-DD format
    for (auto d : dates) {
        std::cout << int(d.year()) << "-"
                  << unsigned(d.month()) << "-"
                  << unsigned(d.day()) << "\n";
    }
}



/*
run:

1996-2-16
2028-6-4
2000-2-1
1996-7-10
1993-9-9
2027-2-4
2005-7-31
1994-2-8
2005-6-2
2016-6-24

*/

 



answered Apr 18, 2025 by avibootz
edited 4 hours ago by avibootz
0 votes
#include <iostream>
#include <vector>
#include <random>
#include <chrono>

// Generate a random day point between two dates
std::chrono::sys_days random_date(std::mt19937 &rng) {
    using namespace std::chrono;

    // Define start and end dates as sys_days
    sys_days start = 1990y/January/1;
    sys_days end   = 2030y/December/31;

    // Compute range in days
    auto range = (end - start).count(); // number of days between

    std::uniform_int_distribution<long long> dist(0, range);
    sys_days randomDay = start + days{dist(rng)};

    return randomDay;
}

int main() {
    std::mt19937 rng(std::random_device{}());
    std::vector<std::chrono::sys_days> dates;

    for (int i = 0; i < 10; ++i) {
        dates.push_back(random_date(rng));
    }

    for (auto d : dates) {
        // Convert sys_days to time_t for printing
        auto tt = std::chrono::system_clock::to_time_t(
            std::chrono::time_point<std::chrono::system_clock, std::chrono::days>(d)
        );
        std::tm tm = *std::localtime(&tt);
        std::cout << (tm.tm_year + 1900) << "-"
                  << (tm.tm_mon + 1) << "-"
                  << tm.tm_mday << "\n";
    }
}



/*
run:

1998-4-10
2021-6-30
2023-8-24
2028-11-8
2029-7-8
1994-10-23
1991-11-29
2014-12-6
1991-3-3
2012-10-25

*/

 



answered 4 hours ago by avibootz
0 votes
#include <iostream>
#include <vector>
#include <random>
#include <ctime>

// Generate a random date between two years using time_t
std::tm random_date(std::mt19937 &rng, int startYear, int endYear) {
    // Convert start and end years to timestamps
    std::tm start = {};
    start.tm_year = startYear - 1900;
    start.tm_mon = 0;
    start.tm_mday = 1;

    std::tm end = {};
    end.tm_year = endYear - 1900;
    end.tm_mon = 11;
    end.tm_mday = 31;

    time_t start_t = std::mktime(&start);
    time_t end_t   = std::mktime(&end);

    // Uniform distribution over the timestamp range
    std::uniform_int_distribution<long long> dist(start_t, end_t);

    time_t random_t = dist(rng);

    // Convert back to tm
    std::tm result = *std::localtime(&random_t);
    
    return result;
}

int main() {
    std::mt19937 rng(std::random_device{}());
    std::vector<std::tm> dates;

    for (int i = 0; i < 10; i++) {
        dates.push_back(random_date(rng, 1990, 2030));
    }

    for (auto &d : dates) {
        std::cout << (d.tm_year + 1900) << "-"
                  << (d.tm_mon + 1) << "-"
                  << d.tm_mday << "\n";
    }
}



/*
run:

1990-5-25
2011-6-13
2006-10-25
1999-11-16
2030-6-6
2004-9-11
2027-3-15
2029-12-13
2010-6-7
1999-9-18

*/

 



answered 4 hours ago by avibootz
...