How to generate a cryptographically secure random integer within a range? : <p>I have to generate a uniform, secure random integer within a given range for a program that generates passwords. Right now I use this :</p>

<pre><code>RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] rand = new byte[4];
rng.GetBytes(rand);
int i = BitConverter.ToUInt16(rand, 0);
int result = i%max;   //max is the range's upper bound (the lower is 0)
</code></pre>

<p>Is this method safe to use for cryptographic purposes ? If not, how should I do it ?</p>
