Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/times/cpp$ _

Generate Random Times in C++

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: C++

What: Generate random times in C++ using std::chrono and std::random. Covers time generation, formatting, and validation.

Try it: Use our interactive Times generator or integrate this code into your C++ application.

Generate random times in C++ using std::chrono and std::random. Covers time generation, formatting, and validation. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , Ruby and Go or use our interactive web generator.

C++ Code Example

#include <iostream>
#include <random>
#include <iomanip>
#include <sstream>
#include <vector>
#include <chrono>

class TimeGenerator {
private:
    std::mt19937 rng;
    std::uniform_int_distribution<int> hourDist;
    std::uniform_int_distribution<int> minuteDist;
    std::uniform_int_distribution<int> secondDist;

public:
    TimeGenerator()
        : rng(std::random_device{}()),
          hourDist(0, 23),
          minuteDist(0, 59),
          secondDist(0, 59) {}

    // Generate random time (24-hour format HH:MM:SS)
    std::string randomTime24() {
        int hour = hourDist(rng);
        int minute = minuteDist(rng);
        int second = secondDist(rng);

        std::ostringstream oss;
        oss << std::setw(2) << std::setfill('0') << hour << ":"
            << std::setw(2) << std::setfill('0') << minute << ":"
            << std::setw(2) << std::setfill('0') << second;
        return oss.str();
    }

    // Generate random time (12-hour format with AM/PM)
    std::string randomTime12() {
        std::uniform_int_distribution<int> hour12Dist(1, 12);
        int hour = hour12Dist(rng);
        int minute = minuteDist(rng);
        std::string ampm = (rng() % 2 == 0) ? "AM" : "PM";

        std::ostringstream oss;
        oss << std::setw(2) << std::setfill('0') << hour << ":"
            << std::setw(2) << std::setfill('0') << minute << " " << ampm;
        return oss.str();
    }

    // Generate random time within business hours (9 AM - 5 PM)
    std::string randomBusinessTime() {
        std::uniform_int_distribution<int> businessHourDist(9, 17);
        int hour = businessHourDist(rng);
        int minute = minuteDist(rng);

        std::ostringstream oss;
        oss << std::setw(2) << std::setfill('0') << hour << ":"
            << std::setw(2) << std::setfill('0') << minute;
        return oss.str();
    }

    // Validate time format (basic validation)
    bool isValidTime(const std::string& time) {
        if (time.length() != 8) return false;
        if (time[2] != ':' || time[5] != ':') return false;

        int hour = std::stoi(time.substr(0, 2));
        int minute = std::stoi(time.substr(3, 2));
        int second = std::stoi(time.substr(6, 2));

        return hour >= 0 && hour < 24 &&
               minute >= 0 && minute < 60 &&
               second >= 0 && second < 60;
    }

    // Generate multiple random times
    std::vector<std::string> generateTimes(int count) {
        std::vector<std::string> times;
        times.reserve(count);
        for (int i = 0; i < count; ++i) {
            times.push_back(randomTime24());
        }
        return times;
    }
};

int main() {
    TimeGenerator generator;

    std::cout << "24-hour: " << generator.randomTime24() << std::endl;
    std::cout << "12-hour: " << generator.randomTime12() << std::endl;
    std::cout << "Business: " << generator.randomBusinessTime() << std::endl;
    std::cout << "Valid: " << (generator.isValidTime("14:30:45") ? "true" : "false") << std::endl;

    auto batch = generator.generateTimes(3);
    std::cout << "Batch: ";
    for (size_t i = 0; i < batch.size(); ++i) {
        std::cout << batch[i];
        if (i < batch.size() - 1) std::cout << ", ";
    }
    std::cout << std::endl;

    return 0;
}

[EXPLANATION]

C++ uses std::uniform_int_distribution for hours (0-23), minutes (0-59), seconds (0-59). std::ostringstream with std::setw(2) and std::setfill('0') formats with leading zeros. std::mt19937 provides quality random generation. std::chrono can represent time durations. Manual validation checks hour, minute, second ranges.

Expected Output

24-hour: 14:23:45
12-hour: 09:15 AM
Business: 11:30
Valid: true
Batch: 08:45:12, 16:20:33, 22:10:05

Common Use Cases

  • Generate times for C++ application testing
  • Create time fields for embedded system testing
  • Generate appointment times for scheduling systems
  • Test time validation in C++ applications
  • Create working hours for time tracking systems

Important Notes

  • std::setw(2) and std::setfill('0') add leading zeros
  • std::chrono::duration can represent time intervals
  • std::stoi() converts string to integer for validation
  • std::mt19937 is Mersenne Twister random generator
  • vector::reserve() pre-allocates for better performance

Try Our Interactive Generator

Don't want to write code? Use our free web-based Times generator with instant results.

TRY TIMES GENERATOR →