Suppose you have a random number generator that generates a random floating point number between [0.0, 1.0), for example drand48 , how can you create a random number generator that generates an integer between [1, n].
drand48
Multiply by n , take the word and add 1.
n
Take the RNG result, multiply by n-1 and add 1.
Example in c:
long rand(int n) { double rand1 = 0; rand1 = drand48(); rand1 *= n - 1; rand1 += 1; return (long)rand1; }
I believe that you may have different random number generators.
Multiply by n, take the int, and add 1.
Source: https://habr.com/ru/post/900532/More articles:Axis: CheckBox checkbox and text are not on the same line - asp.netSpring Social Twitter Oauth - springUsing LPEG (Grammars Lua Parser Expression Grammars), for example boost :: spirit - parsingreuse ++: more efficient if I force right-to-left value? - haskellclearing some session data in an encoder - phpPython will not read sys.argv - pythonMicrosoft SQL Server Management Studio runs a script from within a script - sqlMips how to save user input string - stringGet line numbers from .NET exceptions without .pdb files - c #Saving utf-8 data with Doctrine2 in Symfony2 - mysqlAll Articles