
points to the same string buer containing the result of the second calculation. As a solution, it’s possible to allo-
cate/deallocate string buer each time, but more hackish way is to have several buers (4 are here) and fill the next
each time. The bufno variable here is a buer counter in 0..3 range. Its value increments each time, and its value is
also always kept in limits by AND operation (3 & ++bufno).
The author of this piece of code (seemingly Linus Torvalds himself) went even further and forgot (?) to initialize bufno
counter variable, which will have randomgarbage at the function start. Indeed: no matter, which buer we are starting
each time! This can be mistake which isn’t aect correctness of the code, or maybe this is le so intentionally – I don’t
know.
2.1.3 Getting random numbers
When you write some kind of videogame, you need random numbers, and the standard C/C++ rand() function gives
you them in 0..0x7FFF range (MSVC) or in 0..0x7FFFFFFF range (GCC). And when you need a random number in 0..10
range, the common way to do it is:
X_coord_of_something_spawned_somewhere =rand () % 10;
Y_coord_of_something_spawned_somewhere =rand () % 10;
No matter what compiler do you use, you can think about it as 10 is subtraced from rand() result, as long as there is
still a number bigger than 10. Hence, result is remainder of division of rand() result by 10.
One nasty consequence is that neither 0x8000 nor 0x80000000 cannot be divided by 10 evenly, so you’ll get some
numbers slightly more oen than others.
I tried to calculate in Mathematica. Here is what you get if you write <i>rand()
In []:= Counts[Map[ Mod[#, 3] &, Range[0, 16^^8000 - 1]]]
Out []= <|0 -> 10923, 1 -> 10923 , 2 -> 10922| >
So a number 2 appers slightly seldom than others.
Here is a result for rand() % 10:
In []:= Counts[Map[ Mod[#, 10] &, Range [0, 16^^8000 - 1]]]
Out []= <|0 -> 3277 , 1 -> 3277, 2 -> 3277, 3 -> 3277 , 4 -> 3277,
5 -> 3277, 6 -> 3277 , 7 -> 3277, 8 -> 3276 , 9 -> 3276| >
Numbers 8 and 9 appears slightly seldom (3276 against 3277).
Here is a result for rand() % 100:
In []:= Counts[Map[ Mod[#, 100] &, Range[0, 16^^8000 - 1]]]
Out []= <|0 -> 328, 1 -> 328, 2 -> 328, 3 -> 328, 4 -> 328, 5 -> 328,
6 -> 328 , 7 -> 328 , 8 -> 328 , 9 -> 328 , 10 -> 328, 11 -> 328,
12 -> 328, 13 -> 328, 14 -> 328 , 15 -> 328 , 16 -> 328, 17 -> 328,
18 -> 328, 19 -> 328, 20 -> 328 , 21 -> 328 , 22 -> 328, 23 -> 328,
24 -> 328, 25 -> 328, 26 -> 328 , 27 -> 328 , 28 -> 328, 29 -> 328,
30 -> 328, 31 -> 328, 32 -> 328 , 33 -> 328 , 34 -> 328, 35 -> 328,
36 -> 328, 37 -> 328, 38 -> 328 , 39 -> 328 , 40 -> 328, 41 -> 328,
42 -> 328, 43 -> 328, 44 -> 328 , 45 -> 328 , 46 -> 328, 47 -> 328,
48 -> 328, 49 -> 328, 50 -> 328 , 51 -> 328 , 52 -> 328, 53 -> 328,
54 -> 328, 55 -> 328, 56 -> 328 , 57 -> 328 , 58 -> 328, 59 -> 328,
60 -> 328, 61 -> 328, 62 -> 328 , 63 -> 328 , 64 -> 328, 65 -> 328,
14