About random_device

I'm starting to use the new c++11 library.
And i can't understand this function.
It says it will get a number from a random device.

And if it is not present will be a pseudoRNG.

How can i use it correctly?

Does usb-clocks exsist? If yes, where i can buy them and implementt it in random_device?
A random_device is a true random number generator that does not process pseudo-random numbers. It is part of the random header.


1
2
3
4
5
#include <random>
random_device randomNum;

cout << (randomNum % 5) +1 << endl;//This will produce a random number between 1 and 5

Thats how you would use it, I dont know about "usb-clocks".
Really? It is not that random?
It produces always the same number.
Jakee forgot the parenthesis:

 
cout << (randomNum() % 5) + 1 << endl;


Though with C++11 generators you typically would want to use a distribution instead of the % operator:

1
2
3
4
5
6
7
8
9
10
11
#include <random>
#include <iostream>
using namespace std;

int main()
{
    random_device gen;                          // the engine
    uniform_int_distribution<int>   dist(1,6);  // distribution (between 1-6 inclusive .. like a die roll)

    cout << dist(gen) << endl;      // <- generate a number
}
Last edited on
I'm trying with this code
1
2
3
    random_device rd;
    uniform_int_distribution<int>   dist(1,6);
    cout << dist(rd) << endl;

It generates always the same number.
Oh, i'm using MinGw as a compiler.
http://www.cplusplus.com/reference/random/random_device/entropy/

"If the library implementation employs a random number engine instead of a real-random number generator, the value returned by this function is always zero."
Ooo, that's why!
Do you know, some implementation rather than visual studio(resource eater IDE)?
Topic archived. No new replies allowed.