1

I am trying to get a random number up to a maximum of the scaling factor used, which is 1112, and at a minimum of 1000, which is the shifting value I'm using. However, when I run the program, the output I get is 1127. What am I doing wrong? Thank you.

Here is the code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int n = 0;
    n = 1000 + rand() % 1112;
    cout << n << endl;
}

Here's my output in terminal:

1127
Program ended with exit code: 0
John
  • 49
  • 1
  • 7
  • 5
    Do you know how `rand` works? `rand() % 1112` produces number in the range of `[0, 1111]`. You add a `1000` to that, which turns that, to a range of `[1000, 2111]`. Hence, in general, `a + rand () % x` produces numbers in range of `[a, a+x-1]`. – Algirdas Preidžius Nov 05 '19 at 22:27
  • @Carcigenicate Thank you, yes, that's what I was trying to do. – John Nov 05 '19 at 22:31
  • @AlgirdasPreidžius I didn't understand that that is how it worked. Thanks for the explanation! – John Nov 05 '19 at 22:32
  • 1
    "_I didn't understand that that is how it worked._" I am curious then, where did you "learn" your understanding of how it worked (including the terms like "_scaling factor_"? Since reading the documentation of [`rand`](https://en.cppreference.com/w/cpp/numeric/random/rand) reveals, that it "_Returns a pseudo-random integral value between ​`0​` and `RAND_MAX` (`0` and `RAND_MAX` included)._". `%` is a modulo operator, and `+` is an addition operator. – Algirdas Preidžius Nov 05 '19 at 23:08
  • Note that this method of generating random numbers does not produce uniformly distributed random numbers and that the `rand()` random number generator is generally a poor one. Prefer the random number facilities in `` instead, see https://stackoverflow.com/questions/16153589/generating-a-uniform-random-integer-in-c. – walnut Nov 05 '19 at 23:36
  • @AlgirdasPreidžius I've been using a textbook called _"C++ How To Program"_ (Eighth Edition), by Paul Deitel and Harvey Deitel, to learn C++. It seems pretty verbose in how it explains concepts, so it doesn't get to the point with simplicity . That's where I got the term _"scaling factor"_ , and also where I got an incomplete understanding of rand(). – John Nov 06 '19 at 05:27
  • 1
    @JohnAbdelmalek That book was published March 2011. This is before the publication of C++11, a major new version of C++ with many new core and library features (such as `` instead of `rand()`). The C++ that this book is going to be teaching you is out-dated (i.e. C++98 or C++03). After C++11 there have been already two new versions of the language, C++14 and C++17, and the next version C++20 already has a feature complete draft as well. – walnut Nov 06 '19 at 11:37
  • @uneven_mark Thanks for telling me. Not sure what source to go with now.. something new makes sense of course, just not sure which one.. – John Nov 06 '19 at 19:53
  • @JohnAbdelmalek If this is for self-study and not some course requiring to do things a certain way, then have a look at [the book guide here on stackoverflow](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). If you are learning for a specific course, it is probably not helpful to follow some other material. – walnut Nov 06 '19 at 20:11
  • @uneven_mark It's for self-study , not a specific course. Thank you for the info, I appreciate it. :) – John Nov 06 '19 at 20:15
  • Be careful, even if you use the correct range Formular using a reminder operation is biased and slow, here are some alternatives http://www.pcg-random.org/posts/bounded-rands.html – eckes Nov 06 '19 at 21:05

1 Answers1

-1

Try:

int max = 1112;
int min = 1000;
int range = max - min + 1;
n = rand() % range + min;

Haven't tested. I may have "+1" in the wrong places.

Tas
  • 7,023
  • 3
  • 36
  • 51
UncaAlby
  • 5,146
  • 1
  • 16
  • 19
  • 1
    Your answer would definitely be better without the whole "try this but I haven't tried it" and if you explain what the OP is doing wrong. If you weren't aware, there are a number of online C++ compilers which make trying code very easy. I use [Coliru](http://coliru.stacked-crooked.com/) but there are others too – Tas Nov 05 '19 at 22:40
  • Do. Or do not. There is no try. – user4581301 Nov 06 '19 at 05:54
  • @eckes Clearly if you're looking for cryptographically strong random numbers, you aren't going to use the built-in library random number generator, no matter what language or OS you're using. But if, like probably 98% of everybody who needs it, all you need is a fast-enough, good-enough, mostly random selection for something not earth-shatteringly important -- then using the built-in library function is probably fast-enough, good-enough, and mostly random. And I would expect anybody requiring cryptographically strong random numbers would have asked a totally different question. – UncaAlby Nov 06 '19 at 23:02
  • @Tas, tell me that if you tried it and it doesn't work. I expect it works. – UncaAlby Nov 06 '19 at 23:03
  • Bias free is also needed for non-cryptographic applications. – eckes Nov 06 '19 at 23:03
  • @eckes True, but my point is that anybody who needs that good a random number generator would not have asked the question as asked. However, you are correct that for future reference, the OP should be aware that library functions are not suitable for applications that need something better. – UncaAlby Nov 06 '19 at 23:05
  • 1
    Point taken, however I don’t think it’s good to show known Bad implementations without a warning - people might actually use them ,) – eckes Nov 06 '19 at 23:29