1
int number_generator(bool new_id)
{
    if (new_id) 
    { 
        srand(time(NULL));
        int x = 99999 + (rand() % 999999); 
        return x;
    }
    else 
    {
        throw std::runtime_error("Oops! something went wrong"); 
    }
}

This function generates a random 6 digit number given a true argument. I have compiled it many times, and the number it generates always start with a '1'. What am I doing wrong?

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 4
    **WARNING**: Using [`rand()` can be highly problematic](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) and you’re strongly encouraged to use an appropriate [random number generator facility in the Standard Library](http://en.cppreference.com/w/cpp/numeric/random) that produces high-quality random values. Your use of `time(NULL)` as a random number seed means that this will produce identical results if run in the same second, and on many platforms `rand()` is [*barely* random at all](http://dilbert.com/strip/2001-10-25). – tadman Sep 17 '20 at 22:05
  • 2
    Look at the range you're generating over. Also check what `RAND_MAX` is. – tadman Sep 17 '20 at 22:06
  • 1
    Does this answer your question? [Why does rand() yield the same sequence of numbers on every run?](https://stackoverflow.com/questions/9459035/why-does-rand-yield-the-same-sequence-of-numbers-on-every-run) – Random Davis Sep 17 '20 at 22:06
  • 1
    If you need a number between X and Y consider using [`std::uniform_int_distribution`](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution). – tadman Sep 17 '20 at 22:07
  • 1
    `time(NULL)` typically has a resolution of 1 second. If you call `srand` multiple times within 1 second you will receive the same seed and generate the same sequence with `rand`. Since the function looks like it either reseeds and processes the first number in the sequence or throws an exception, this function's pretty much worthless as a random number generator if called repeatedly and quickly. See [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) for more details. – user4581301 Sep 17 '20 at 22:22
  • rand() Considered Harmful - Stephan T. Lavavej - https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful – Richard Critten Sep 17 '20 at 22:29

2 Answers2

5

rand() returns a number between 0 and RAND_MAX. The latter can be as low as 32767. C++ only requires RAND_MAX to be at least this large. A C++ implementation could use a larger value, but that is not mandated by the standard.

It is possible (even likely) that your implementation chose the minimum admissible value for RAND_MAX, i.e. 32767. If that is the case, your computed value 99999 + (rand() % 999999) is equal to 99999 + rand(), hence it lies between 99999 and 99999 + 32767. This will always start with a digit 1 except for the rather unlikely case where rand() is zero, causing the result to be 99999.

chi
  • 111,837
  • 3
  • 133
  • 218
0

std::rand returns a pseudo-random integral value between ​0​ and RAND_MAX. That number modulo 999999

rand() % 999999

rand() has to return a number in range [100001, 999998] to have a 2 as starting digit. There is no guarantee rand() will return something in this interval, check RAND_MAX on your system.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
  • You could add that there is no guarantee that `rand()` can ever return a number in that interval. The rest is very clear to me. (already +1'd) – chi Sep 17 '20 at 22:23