help!random_deivce

i just want to generate a random number, and copy a code from others:

#include <iostream>
#include <random>
int main()
{
std::random_device rd;
for(int n=0; n<20000; ++n)
std::cout << rd() << std::endl;
return 0;
}

however, it show runtime errow!
showing as:
terminate called after throwing an instance of 'std::runtime_error'
what<>: random_device::random_device<const std::string&>


does anyone can help me?

it is very important for me to generate a random and that can be used repeatly.
thanks very much.

i use windows 7 and codeblock.
You are using outdated MinGW version which does not support random_device.
Upgrade.
Do not use std::random_device without having first verified that it is a true random device on the particular implementation. For instance, the default-constructed GCC MinGW implementation of std::random_device generates the same predictable sequence each time the program is run.

Perhaps something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstdint>
#include <random> // http://en.cppreference.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://www.cplusplus.com/forum/beginner/103051/#msg554829
    try
    {
        std::random_device rdev ;
        if( rdev.entropy() < 0.05 ) throw std::runtime_error( "not a 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' ;
}

http://ideone.com/ks0vgD
thanks both of you very much. i will go to add and fix into my program.
Topic archived. No new replies allowed.