O
O
Old Odessa2016-02-09 19:14:27
C++ / C#
Old Odessa, 2016-02-09 19:14:27

How does the rand() function work?

I don’t understand what exactly the rand() function generates - why from 0 to rand_max, and why we set such a range.
a+rand()%(ba)
I don't understand why use the remainder of the division.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Martyanov, 2016-02-09
@vs_convoy

The function generates a pseudo-random number in the range [0, RAND_MAX), this is a given given in the documentation. The formula you gave is obviously getting a number in the range [a, b]. As for the inclusion of borders, however, I'm not sure ...

A
abcd0x00, 2016-02-10
@abcd0x00

I don't understand what exactly the rand() function generates

rand() generates a single integer.
The operation of taking the remainder of a division is used to set the upper limit of a number.
If you calculate 12345% 7, then the result will be less than 7.
If you calculate 12346% 7, then the result will be less than 7.
If you calculate 123456789% 7, then the result will be less than 7.
The operation of taking the remainder of dividing one number by another has nothing to do with the rand function () does not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question