Java nextInt .
, . , . .
( ) # :
public static int GetNextInt32(this RNGCryptoServiceProvider rng, int maxValue)
{
if (maxValue < 1)
throw new ArgumentOutOfRangeException("maxValue", maxValue, "Value must be positive.");
var buffer = new byte[4];
int bits, val;
if ((maxValue & -maxValue) == maxValue)
{
rng.GetBytes(buffer);
bits = BitConverter.ToInt32(buffer, 0);
return bits & (maxValue - 1);
}
do
{
rng.GetBytes(buffer);
bits = BitConverter.ToInt32(buffer, 0) & 0x7FFFFFFF;
val = bits % maxValue;
} while (bits - val + (maxValue - 1) < 0);
return val;
}