2

I am trying to generate a list of 10 randomly generated numbers in the range of 100000 to 583662 and every thing I have tried results in all numbers beginning with a 1 and no numbers higher than 199999 are ever generated. I have actually generated 1000 numbers to see if maybe it was just too small of a sample, but in 1000 numbers I should have had at least a few numbers higher than 199999, but none were. Here is the code I hae been using for this:

    #include <stdio.h>
    #include <cstdlib>
    #include <ctime>
    #define SIZE 100

    int main()
    {
        int globalArray[SIZE] = {0};
        srand((unsigned)time(0));
        int count;
        for (int count = 0; count < 100; ++count) {
            globalArray[count] = (rand() % 483662) + 100000;
        }
        for (int count = 0; count < 100; ++count) {
            printf("%d\n", globalArray[count]);
        }
        return 0;
    }
  • The `random` header has what you need to do this. – Brady Dean Apr 19 '20 at 00:21
  • 3
    This is one of the reasons why you don't use `rand`. `RAND_MAX` can be as small as 32767. – eesiraed Apr 19 '20 at 00:21
  • Related: [Why is the new random library better than std::rand()?](https://stackoverflow.com/q/53040940/9716597) – L. F. Apr 19 '20 at 00:23
  • @BessieTheCow So what other function can be used to generate the range of random numbers I am looking for? – Stephen Dangel Apr 19 '20 at 00:31
  • Stuff in the C++ [`` header](https://en.cppreference.com/w/cpp/header/random), such as [`std::uniform_int_distribution`](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) with a properly seeded generator. – eesiraed Apr 19 '20 at 00:32
  • An entertaining and educational presentation on just how much worse than the modern alternatives `rand` is: [Rand Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – user4581301 Apr 19 '20 at 01:20

2 Answers2

4

You can use the new <random> library instead of std::rand and improve the readability of your code:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>

int main()
{
    std::mt19937 engine{std::random_device{}()};
    std::uniform_int_distribution dist{100000, 583662}; // inclusive

    std::generate_n(std::ostream_iterator<int>{std::cout, "\n"}, 1000,
                    [&] { return dist(engine); });
}

(live demo)


To generate the numbers into an array, per comment:

std::generate_n(globalArray, SIZE, [&] { return dist(engine); });

or:

std::generate(std::begin(globalArray), std::end(globalArray), [&] { return dist(engine); });

Even simpler with C++20 ranges or ranges-v3:

namespace ranges = std::ranges; // C++20
ranges::generate(globalArray, [&] { return dist(engine); });
L. F.
  • 19,445
  • 8
  • 48
  • 82
0

You should rather use:

rand() / (float)RAND_MAX * range

From, platform to platform, RAND_MAX varies. If your RAND_MAX is smaller than 483662, you won't get the full range.

An alternative would be:

(rand() + rand() * RAND_MAX) % range
Jeffrey
  • 11,063
  • 1
  • 21
  • 42
  • 1
    That just hides the underlying problem of `rand` not generating enough entropy. You'll get numbers which are spread out but only a fraction of the numbers in the range can be generated. – eesiraed Apr 19 '20 at 00:24
  • good point. Might still be useful, depends on the domain where the numbers are used. – Jeffrey Apr 19 '20 at 00:24