0

So I'm practicing classes and headers. I'm trying to create a simpler random number generator by putting it into another class. The source code compiles without any errors. However, as you can see in the source code I use time(0) as a seed to generate random numbers. Yet somehow the seed doesn't change when I restart the program multiple times to test the randomness. The seed is apparently the same. Why is this ?


main.cpp

 #include <iostream>
 #include "RandomNumber.h"

 using namespace std;

 int main(){

    RandomNumber rand;
    int a = 10000;
    int randomnumber = rand.RandNumGen(1, 100);

    cout << "10 000 - a random number between 1 and 100 looks like this\n" << a << " - " << randomnumber << " = " << a - randomnumber << endl;
 return 0;
 }

RandomNumber.h

 #include <iostream>
 #include <ctime>
 #include <random>

 using namespace std;



 class RandomNumber
{
public:
    RandomNumber();
    int RandNumGen(int minimum, int maximum);

protected:

private:
};

#endif

RandomNumber.cpp

 #include "RandomNumber.h"

 using namespace std;

 RandomNumber::RandomNumber(){

 }

 int RandomNumber::RandNumGen(int minimum, int maximum){

 default_random_engine generator(time(0));

 uniform_int_distribution<int> RNG(minimum, maximum);

 return RNG(generator);
 }

In short. The RandNumGen function in the RandomNumber class returns the same random number even though the seed is constantly changing (using time in seconds).

Since the question was marked as a duplicate. I'm not asking why the same seed gives the same number. I'm asking why the time(0) seed is NOT changing, time is always progressing so the random number between 1 and 100 should to right ?

Tarmiac
  • 846
  • 1
  • 8
  • 14
  • 2
    Why are you re-seeding each time you generate a number? – jxh Jul 18 '16 at 20:05
  • If there is another way of getting different random numbers every time the function is called, please let me know how. – Tarmiac Jul 18 '16 at 20:07
  • 1
    `time(0)` is a horrible seed. 1) predictable 2) identical for two invocations of the program within the same second. 3) very few bits change even if there are hours between runs. Research `std::random_device` and `std::seed_seq`. – Jesper Juhl Jul 18 '16 at 20:07
  • 2
    Be careful with `std::random_device` if using mingw. Last time I checked it always returned the same number. – user4581301 Jul 18 '16 at 20:11
  • @user4581301 There's a reason I mentioned `seed_seq`. If one is paranoid (or just faced with an unreliable `random_device`) one can always combine multiple (potentially (or known) bad) sources such as; `random_device()`, `time(0)`, `getpid()`, `argc`, `gettid()`, `getcwd()`, `uptime()`, and more, and be fairly confident that the resulting seed is good-ish. – Jesper Juhl Jul 18 '16 at 20:19
  • FYI, I tried manually inputting seeds and it gave different numbers. I tried printing time(0) in the console and it displays different time every compilation as well. – Tarmiac Jul 18 '16 at 20:26
  • @Felix Willis There is. Seed *once*, then re-use the seeded generator - don't re-create it. And please, don't use `default_random_engine` - you don't know what it is. Pick a suitable one for your purpose and name it explicitly (recommended for most purposes (though not cryptographic ones): `mt19937`). – Jesper Juhl Jul 18 '16 at 20:27
  • @Felix Willis of course `time(0)` gives different output every time you rebuild and run your application - you hardly do that within a single second - right?. But two users could easily run your final app at the same time (within the same second) and thus get the same seed. Besides, guessing the start time of an application within a margin that allows one to brute-force the seed if one knows it is `time(0)` (easily discoverable) is trivial. Hence; bad seed. – Jesper Juhl Jul 18 '16 at 20:32
  • @JesperJuhl Well thanks for the tip. But this question is purely technical. Why doesn't a different seed generate a different random number ? – Tarmiac Jul 18 '16 at 20:37
  • A unique seed *will* generate a different random number (well, with the caveat that of course *some* seeds will produce identical starting numbers for some generators - even if quite unlikely, but you shouldn't be using just the first number of the sequence *anyway* - aka not re-creating your generator constantly). – Jesper Juhl Jul 18 '16 at 20:42

0 Answers0