fork(1) download
#include <iostream>
#include <cstdint>
#include <random> // http://e...content-available-to-author-only...e.com/w/cpp/numeric/random
#include <ctime>
#include <stdexcept>

int main()
{
    constexpr std::size_t NSEEDS = 5 ;
    std::uint32_t seeds[NSEEDS] = { 0 } ;

    // see: http://w...content-available-to-author-only...s.com/forum/beginner/103051/#msg554829
    try
    {
        std::random_device rdev ;
        // if( rdev.entropy() < 0.05 ) throw std::runtime_error( "not a random device" ) ;
        // commented out because GCC reports zero entropy even for a true std::random_device
        for( auto& s : seeds ) s = rdev() ;
        std::cout << "seeds generated with a random device\n" ;
    }
    catch( const std::exception& )
    {
        std::cout << "no random device / bogus random device / random device has no entropy\n" ;
        auto t = std::time(nullptr) ;
        for( std::size_t i = 0 ; i < NSEEDS ; ++i )
            if( seeds[i] == 0 ) seeds[i] = t * (i+3) ;
    }

    std::seed_seq seed_seq( seeds, seeds+NSEEDS ) ;
    std::mt19937 twister(seed_seq) ;
    std::uniform_int_distribution<int> dist( 1, 1000000 ) ;

    for( int i = 0 ; i < 10 ; ++i ) std::cout << dist(twister) << ' ' ;
    std::cout << '\n' ;
}
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
seeds generated with a random device
811479 64016 360491 298008 779442 293644 673381 467266 522115 134440