1

Alright so, I'm a noob first of all. I started studying code (in C++), and I want to make a random number generator. It's great and all, but as far as I've observed, the generated numbers never exceed the "int" limit of 32768, even tho my variables are all "unsigned long long" (I'm pretty sure that's how you get the largest pool of numbers). I'm pretty sure it's something small, but it;s been bothering me for a day, and I really need answers. Here's how my current code looks like :

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    unsigned long long n,m,r,mx;
    cout<< "Please Enter The Number Of Desired Randomly Generated Numbers : ";
    cin>>m;
    cout<< "Please Enter An Upper Limit to the Random Numbers : " ;
    cin>>mx;
    srand ( time(NULL) );
    for (int i=1; i<=m ; i++)
    {
        n = rand() % mx;
        cout << n  << endl; 
    }
    cout<< "Rate this Program Out Of 10: ";
    cin >> r;
    cout << r << " " << "/" << "10";
        return 0;
}
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
NRDSTRM
  • 13
  • 3
  • rand() return an integer with is smaler then RAND_MAX. – gerum Aug 03 '21 at 21:02
  • 2
    https://stackoverflow.com/questions/53040940/why-is-the-new-random-library-better-than-stdrand – Retired Ninja Aug 03 '21 at 21:04
  • 2
    And rand() is a C function. The C++ way to get random numbers is this: https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution – gerum Aug 03 '21 at 21:06
  • I don't have a rand_max antwhere. Where do I add it? – NRDSTRM Aug 03 '21 at 21:06
  • you need to read some documentation of stuff you are using. Nobody can remember everything. See [here](https://en.cppreference.com/w/cpp/numeric/random/rand): "Returns a pseudo-random integral value between ​0​ and RAND_MAX (0 and RAND_MAX included). " and RAND_MAX is a link to documentation for RAND_MAX. I could understand when that does not answer your quesiton, but it would be a start – 463035818_is_not_an_ai Aug 03 '21 at 21:08
  • It's `RAND_MAX` not `rand_max`. – Costantino Grana Aug 03 '21 at 21:08
  • 3
    The type of the variable you assign the result to does not change the return type of `rand()` or the range of values `rand()` can return. – Fred Larson Aug 03 '21 at 21:28
  • 2
    Some tips about `C++`: Don't use `using namespace std;`, looks good, but sometimes it breaks stuff. Badly. And it can be hard to find, if you don't expect it. `` is a `C` header. For `C++` `` should be used. For the `std` part:https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Lala5th Aug 03 '21 at 21:36
  • Obligatory link to Lavavej's [rand() Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) presentation on the problems you can run into with `rand` and how to use the `random` library added in C++11. – user4581301 Aug 03 '21 at 21:47

1 Answers1

1

Even though all the numbers you use are unsigned long long, rand() will only ever return a number less than or equal to RAND_MAX which is guaranteed to be 32767 or more.

To guarantee a return value more than 32767 you're going to need some more advanced random number generation techniques. The standard library has a module for this called random.

Take a look at the uniform_int_distribution object. That page gives an example of how to use it to generate regular integers however the object does take a template parameter that allows you to specify what kind of integer is returned.

In your case you would want to use: std::uniform_int_distribution<unsigned long long>

Using that in the example on the page will generate 10 unsigned long long numbers (however if you copy the example exactly they will still be limited to between 1 and 6).

WalleyM
  • 172
  • 7