Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:28:50+00:00 2026-06-15T14:28:50+00:00

I have come with the following coding to generate range within 1000-9999: Dim byt

  • 0

I have come with the following coding to generate range within 1000-9999:

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

It shows the error:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: startIndex

i have done following code. is it correct?

Public Sub secrand()

    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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:28:51+00:00Added an answer on June 15, 2026 at 2:28 pm

    The second parameter to BitConverter.ToInt32 is an offset into the byte array. It should be 0 for you. There is also no reason to get 1000 byttes if you’re using only 4 of them.

    A working version in C#:

    public static int RandomUniform(int count)
    {
        if(count <= 0)
             throw new ArgumentOutOfRangeException("count");
        var rng=new RNGCryptoServiceProvider();
        var bytes=new byte[8];
        rng.GetBytes(bytes, 0);
        return BitConverter.ToUInt64() % (uint)count;
    }
    
    Label4.Text = 1000 + RandomUniform(9000);
    

    In VB.net this will look similar to

    Public Shared Function RandomUniform(count As Integer) As Integer
        If count <= 0 Then
            Throw New ArgumentOutOfRangeException("count")
        End If
        Dim rng = New RNGCryptoServiceProvider()
        Dim bytes = New Byte(8) {}
        rng.GetBytes(bytes, 0)
        Return BitConverter.ToUInt64() Mod CUInt(count)
    End Function
    

    This code has two weaknesses:

    1. It’s quite slow. The per-call overhead of RNGCryptoServiceProvider.GetBytes is large.
      For better performance use a buffer of several kilobytes and only call GetBytes when it’s used up.
    2. It’s not perfectly uniform, but the bias should be small enough for most applications.

      Even when using a value for count that exhibits maximal bias you will need over ten billion samples to detect it, whereas for System.Random a handful are enough. i.e the bias is about 8 billion times smaller than for System.Random.

    If either weakness is unacceptable for you, you can look into my random number generator project which offers fast, secure and unbiased numbers. But this comes at the cost of adding an external library instead of simply writing a few lines of code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to coding in php and have come accross a bit
I have come across the following basic Tree conflict: Local add, incoming add upon
I have come across the following jQuery code but could not understand it. What
I have come across the following link: http://code.google.com/p/a-star/source/browse/trunk/java/PathFinder.java?r=8 I have got the code working
I am writing an API and have come across the following pattern: My API
I am trying to configure TFS 2010 reporting and have come accross the following
I have come up with the following code to call a webservice using client/server
So I have come up with the following style sheet in jsfiddle http://jsfiddle.net/8FNZE/2/ and
I need to do the following .. have come across various examples but i
I have come across the following type of code many a times, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.