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

Generate Random Timestamps in C++

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: C++

What: Generate random Unix timestamps in C++ using std::chrono and std::random. Covers current time, past/future timestamps, and formatting.

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

Generate random Unix timestamps in C++ using std::chrono and std::random. Covers current time, past/future timestamps, and formatting. 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 <chrono>
#include <iomanip>
#include <sstream>
#include <vector>

class TimestampGenerator {
private:
    std::mt19937_64 rng;

public:
    TimestampGenerator() : rng(std::random_device{}()) {}

    // Generate current Unix timestamp (seconds)
    int64_t currentTimestamp() {
        auto now = std::chrono::system_clock::now();
        return std::chrono::duration_cast<std::chrono::seconds>(
            now.time_since_epoch()
        ).count();
    }

    // Generate current Unix timestamp (milliseconds)
    int64_t currentTimestampMillis() {
        auto now = std::chrono::system_clock::now();
        return std::chrono::duration_cast<std::chrono::milliseconds>(
            now.time_since_epoch()
        ).count();
    }

    // Generate random timestamp between two Unix timestamps
    int64_t randomTimestamp(int64_t start, int64_t end) {
        std::uniform_int_distribution<int64_t> dist(start, end);
        return dist(rng);
    }

    // Generate random past timestamp (within N days)
    int64_t randomPastTimestamp(int daysAgo = 365) {
        int64_t now = currentTimestamp();
        int64_t seconds = daysAgo * 86400LL;
        std::uniform_int_distribution<int64_t> dist(now - seconds, now);
        return dist(rng);
    }

    // Generate random future timestamp (within N days)
    int64_t randomFutureTimestamp(int daysAhead = 365) {
        int64_t now = currentTimestamp();
        int64_t seconds = daysAhead * 86400LL;
        std::uniform_int_distribution<int64_t> dist(now, now + seconds);
        return dist(rng);
    }

    // Convert timestamp to readable format
    std::string formatTimestamp(int64_t timestamp) {
        std::time_t time = static_cast<std::time_t>(timestamp);
        std::tm* tm = std::localtime(&time);
        std::ostringstream oss;
        oss << std::put_time(tm, "%Y-%m-%d %H:%M:%S");
        return oss.str();
    }

    // Generate multiple random timestamps
    std::vector<int64_t> generateTimestamps(int count, int64_t start, int64_t end) {
        std::vector<int64_t> timestamps;
        timestamps.reserve(count);
        for (int i = 0; i < count; ++i) {
            timestamps.push_back(randomTimestamp(start, end));
        }
        return timestamps;
    }
};

int main() {
    TimestampGenerator generator;

    std::cout << "Current: " << generator.currentTimestamp() << std::endl;

    // 2023-01-01 to 2024-12-31 in Unix timestamps
    int64_t start = 1672531200; // 2023-01-01
    int64_t end = 1735689599;   // 2024-12-31
    std::cout << "Random: " << generator.randomTimestamp(start, end) << std::endl;

    int64_t past = generator.randomPastTimestamp(30);
    std::cout << "Past: " << past << " (" << generator.formatTimestamp(past) << ")" << std::endl;

    int64_t future = generator.randomFutureTimestamp(30);
    std::cout << "Future: " << future << " (" << generator.formatTimestamp(future) << ")" << std::endl;

    auto batch = generator.generateTimestamps(3, start, end);
    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++ std::chrono::system_clock::now() gets current time. time_since_epoch() converts to duration. duration_cast converts to seconds or milliseconds. std::uniform_int_distribution generates random values. std::put_time() formats timestamps. std::mt19937_64 provides 64-bit random generation for large timestamp ranges. Use std::gmtime() instead of localtime() for UTC.

Expected Output

Current: 1703001234
Random: 1698765432
Past: 1700445678 (2023-11-19 14:27:58)
Future: 1706123456 (2024-01-24 18:10:56)
Batch: 1680123456, 1685432100, 1692345678

Common Use Cases

  • Generate timestamps for C++ application testing
  • Create event timestamps for embedded system testing
  • Generate log timestamps for high-performance systems
  • Test timestamp-based protocols in networking code
  • Create expiration timestamps for cache implementations

Important Notes

  • std::chrono is the modern C++ time library (C++11+)
  • std::mt19937_64 uses 64-bit Mersenne Twister for large ranges
  • std::put_time requires <iomanip> header
  • Use std::gmtime() for UTC, std::localtime() for local time
  • 86400LL uses long long literal for 64-bit multiplication

Try Our Interactive Generator

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

TRY TIMESTAMPS GENERATOR →