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:
srand(time(NULL));

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”;
srand(time(NULL));
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.

https://en.cppreference.com/w/c/numeric/random/rand
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

Thanks much! This has helped muchly. Thanks for the patience! :slight_smile:

As mentioned above (pseudo) random number generation is not trivial.

Depends on what type of application, there are types of pseudo random algorithms that give desirable properties for some things and not for others. So whenever you end up needing random, just consider that the random function can be substituted by x algorithm, where x is some well-known strategy to a problem.

For example std::mersenne_twister_engine - cppreference.com is not cryptographically secure, and has other critiques:
algorithms - Why is the Mersenne Twister regarded as good? - Computer Science Stack Exchange

Obviously for the beginning the basic rand() is fine, just planting the seed that should you need it for other applications (or should you want to study what goes on behind the sauce), there’s a lot more to consider.

2 Likes