C++ random function questions

I have a few questions about the random function in C++. I’ve finished the assignment (magic 8 ball), but I just don’t have a lot of insight into how this works.

First, you define a variable:
int answer = std::rand() % 10

Random modulo 10, returns the remainder. This is intended to make a random number from 0-9. I understand how the modulo works, but I don’t understand what it’s dividing, and how this can be guaranteed to return a number from 0-9. If the start number is 3,993,826, it won’t return 0-9, so what is it working on?

A little bit later we add the line:

So my stab is it has something to do with the clock. Also, with this line, why is it placed where it is in the program? At this point the program essentially looks like this:

int main() {
std::cout <<“MAGIC 8 BALL : \n\n”;
int answer = std::rand() % 20;
std::cout << answer;

why is that NULL line above the variable line? Is there a number already “there” or being accessed that needs to be cleared? Also, earlier in the program it was % 10 and with no explanation, it was changed to % 20. Is there any significance there?

Sorry for the bazillions of questions…I don’t want to just blindly continue by rote—I want to know HOW this stuff works. And I’m an idiot, not a math person so use short words and crayons when you answer. :confused: I watched a video series on C++ from Giraffe Academy that got me off to an excellent start and I’ve been relying on my knowledge from there so far, but this is the first truly new concept I’ve found so far (not a complaint, that!) and I don’t feel like I’ve gained any insight into how the random function actually works.

thanks very much. leaves appropriate offerings at the Computer Godz altar, bows, leaves

Something to consider about computer generated random numbers is that when the computer is first turned on, everything is initialized to some fixed state, which state includes the seed that the random number generator begins with. Let’s say we generate a sequence of numbers, save them (or line print them), then restart the computer. What will happen if we generate a random sequence the same way as the first time? Chances are it will be the same sequence as before. Why? Because it started from the same seed.

srand() is a way to counteract that, at least in so far as we can not predict the sequence that will unfold unless we know we used the same seed.

time(NULL) is the number of seconds since January 1, 1970. Of course that number changes every second and if we use that function to seed our random number generator, it will always yield a new sequence.

As for the modulo, that is a ratio telling C how many numbers in the sampling sequence. % 10 means select from numbers between 1 and 10; % 20 selects from 1 to 20; and so on.

1 Like

Thanks very much for the answer!

So it sounds like I can take that literally–when a computer is FIRST turned on, for the very first time. Not just the first today, but ever. Boot number one. Is that right?

And using the srand() and time(NULL) is sort of like telling C++ “no, don’t use the seed from the first boot–generate a number THIS way” and base it on number of seconds since 1970.

Also, using the modulo operator in the context of an srand() statement sorta kinda changes it’s meaning then, right? Nothing to do with division, it’s all about the range you want it to stay in.

thanks very much for your time!

1 Like

Every cold start, not just the first. A lot of the initialization is booted from the ROM on the mother board during the POST phase, before the DOS is even running. Using the clock to set the seed means that seed has never been used before.

The modulo is used with rand(), not srand(). When we say, ‘ratio’, think, 1 of 20 or one of whatever is after the modulo operator, so, yes, it is not about division, just the sample size, N:

rand() % N

I don’t know C++ well enough to say whether zero is in that sample. Something for you to research.

The docs have all the answers you need on std::rand.
Reading documentation can be daunting as a newcomer. It will become easier with repetition.

1 Like