generate random int using c++0x <random>

1
2
3
4
5
6
7
8
9
#include <random>
int main() {
	mt19937 rng1;
	// default_random_engine rng2;

	uniform_int_distribution<int> distribution(1,100);

	cout << distribution(rng1)<< endl;
}

I'd like to get a number between 1 and 100, but it always output "13".
Seed the rng. For example,

1
2
3
4
5
6
7
8
#include <random>
#include <ctime>

int main() 
{
	std::mt19937 rng1( std::time(nullptr) ) ;
	// ...
}

Thanks.
Pure C++11:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <random>
#include <chrono>

int main()
{
    std::random_device rdev ; // if we have a random_device
    std::mt19937 rng1( rdev() ) ;
    // ...

    // ticks from clock with the highest possible resolution 
    std::mt19937 rng2( std::chrono::high_resolution_clock::now().time_since_epoch().count() ) ;
    // ...
}

Topic archived. No new replies allowed.