Is there a way to get run-time random numbers?

Ok so this isn't as much of a there's a bug in my code problem as it is a how do I even do this problem. The problem is that I need some form of algorithm to get a random number. I've tried rand() but it doesn't seem to work. The number needs to be from 1 to 16 so rand() would be ideal but again rand() doesn't seem to be random at all. I need the type of random that every time I run the .exe file it makes a new random number. If you have any solutions then please share them with me.
Last edited on
You can seed the random-number generation with a random seed first using srand(). See, for example,
http://www.cplusplus.com/reference/cstdlib/srand/

srand (time(NULL));
will set up a seed based on when you are running the program - and you can't really repeat that.

Without a call to srand, rand() will give you a sequence of "random" numbers, in the sense that they are independent of each other, but always the same set!
closed account (ybf3AqkS)
Here is another way

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

int main()
{
    mt19937 re(time(nullptr));
    uniform_int_distribution<> dist(1, 16);
    cout << dist(re) << '\n';
}

Last edited on
Topic archived. No new replies allowed.