Generate random numbers

I am trying to make a very simple console application that just prints out random numbers.

Could someone please help me out with this? maybe show an example program? :)

1
2
3
4
5
6
        #include <random>
  	std::default_random_engine generator;
	std::uniform_int_distribution<int> distribution(1, 6);
  	int dice_roll = distribution(generator);
	
	std::cout << dice_roll;


I have tried this, but each time I compile the program the same number appear each time. Do i have to reset the integer in some way?

All help is greatly appreciated :D
closed account (j3Rz8vqX)
A non c++11 implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdlib>//for use of srand, to increase randomness
#include <ctime>//for use of time, to random seed
using namespace std;
int main()
{
    srand(time(0));//Possibly random
    cout<<"Randomly generating up to 20 'random' integers:"<<endl;
    for(int i=0;i<rand()%20+1;i++)
        cout<<rand()%100+1<<" ";
    cout<<endl;
    cout<<"Custom Exit: Press enter: ";
    cin.get();
    return 0;
}

Edit:
Other than that, the below provides the detail on <random>:
http://www.cplusplus.com/reference/random/
1
2
3
4
5
6
7
8
9
10
11
12
//------------------Setup----------------------------------------------;
//Pseudo-random number engine:
std::default_random_engine generator;
//http://www.cplusplus.com/reference/random/default_random_engine/

//Distribution uniform:
std::uniform_int_distribution<int> distribution(1,6);//Lower bound(1), upper bound(6);
//http://www.cplusplus.com/reference/random/uniform_int_distribution/

//------------------Using----------------------------------------------;
//Uniform distributor called with random engine as a parameter: 
int dice_roll = distribution(generator);
Last edited on
Thank you :D
Topic archived. No new replies allowed.