Next: http://www.fredosaurus.com/notes-cpp/misc/random.html
It mentions that if we want to create a random number in a range 1-10, we can do the following:
1-10
r = (rand() % 10) + 1;
Why are we adding 1? Can you just explain how this process works?
1
And as for the initialization of the random number generator, he mentioned the following:
srand(time(0));
Can you explain this process? And what happens if we do not initialize at all?
Thank.
You add 1 because you need a random number of 1-10, not 0-9, which will be %without +1.
%
+1
, 10 % 10 == 0 9 % 10 == 9, 0-9.+1 "" 1-10- > 10 % 10 + 1 == 1 9 % 10 + 1 == 10
10 % 10 == 0
9 % 10 == 9
10 % 10 + 1 == 1
9 % 10 + 1 == 10
: , srand . rand() , srand "" , rand(). , time(0) , , rand()
srand
rand()
time(0)
rand() int [0, RAND_MAX]. rand() % 10 int [0,9], x k k-1. 1 [1,10].
int
RAND_MAX
rand() % 10
( rand() % k . , .. RNG Boost.)
rand() % k
srand(time(0)) , . t t + 1s, , . , , , . , C . , , , rand() .
srand(time(0))
(, , RNG. Linux BSD /dev/random, "" . , .)
/dev/random
"modulo", . rand() , rand() % 10 - 0 9. 1, 1 10
, . srand(time(0)) "" , . , , , -, , , .
1?
10, 0-9. , , 1.
10
0-9
random without ++ 1 0 9, C, 0 n, . 0, 1-10 (1).
. srand (time (0)), , .
, , , .
, , , srand (0) , . , (0), . .
srand(), , , , , - , ( srand (0)). time (0) , , run to run, srand().
. , , , . , 5, 10 , . , ( !), () - () , .
: , , , / , "5 10" . .
Source: https://habr.com/ru/post/1789715/More articles:Как изменить каталог макета по умолчанию? - ruby-on-railstesting wcf service in browser - c #The superiority of bison does not work - bisonMonotouch Unit Testing - nunitWhy didn't Microsoft turn on SQL Server support in Silverlight? - sql-serverhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1789716/erlangotp-changes-to-the-already-existing-modules&usg=ALkJrhgmEIi5MW5TlljPvKDZfXjItCrekAJQuery ajax proxy - javascriptSQL, Analysis Services, or Reporting Services - sqlHow to create an array of elements from a list tag (ul)? - phphttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1789720/does-tomcat-use-cached-versions-of-plsql-modules&usg=ALkJrhjO03avyKsOl29FGDZ1Mn11zhER4wAll Articles