title: generating random number with RNGCRYPTOSERVICEPROVIDER within specified range
body: <p>I have come with the following coding to generate range within 1000-9999:</p>
  
  <pre><code>Dim byt As Byte() = New Byte(1000) {}
  
  Dim rngCrypto As New RNGCryptoServiceProvider()
  rngCrypto.GetBytes(byt)
  
  Dim randomNumber As Integer = BitConverter.ToInt32(byt, 9999)
  Label4.Text = randomNumber
  </code></pre>
  
  <p>It shows the error:</p>
  
  <blockquote>
    <p>Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: <code>startIndex</code></p>
  </blockquote>
  
  <p>i have done following code. is it correct?</p>
  
  <p>Public Sub secrand()</p>
  
  <pre><code>    Static r As Random = Nothing
  
      If r Is Nothing Then
          Dim seed() As Byte = New Byte(4) {}
          Dim rng As New RNGCryptoServiceProvider
          rng.GetBytes(seed)
          r = New Random(BitConverter.ToInt32(seed, 0))
          Label4.Text = r.Next(1000, 9999)
      End If
  End Sub
  </code></pre>