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 6680215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:26:55+00:00 2026-05-26T04:26:55+00:00

I have found few interesting things (for me at least) while generating random integers

  • 0

I have found few interesting things (for me at least) while generating random integers and I cannot explain it to myself so I thought I will post it here.

My needs were simple: I was generating random integral (Int32) IDs and was aiming to minimize collisions. The generating time wasn´t an issue.

I have tried these methods for generating random integers:

1.)

   return rnd.Next();

where rnd is class field of type Random with Seed from Method #3.

2.)

   return rnd.Next(Int32.MinValue, Int32.MaxValue);

where rnd is again class field of type Random with Seed from Method #3.

3.)

   var buffer = new byte[sizeof(int)];
   using (var rng = new RNGCryptoServiceProvider())
   {
        rng.GetBytes(buffer);
   }            
   return BitConverter.ToInt32(buffer, 0);

Note: I also tried to have that RNGCryptoServiceProvider as class field initialized once on initialization of the containg class to ease work of GC, but I took same time to generate so I thought this would be “more random”.

4.)

   return new Random(Method3()).Next();

5.)

   return new Random(Method3()).Next(Int32.MinValue, Int32.MaxValue);

I know, that creating new Random(int seed) on every call is time consuming, but there is less collision, right?

Well, now the mystery part. I was asuming that most collisions would have the method #1 and #2 where #1 would be slightly faster and more collisionless and least collisions would have the method #4 and #5 where #4 would be slightly faster and more collisionless and method #3 would be some sort of compromise.

So I made a test to prove my asumptions. I generated 10x (to make it average) one million of random numbers using every mentioned method and took the average number of collision and average time it took to generate one million numbers. The results (below) are a bit suprise to me.

The results:duration is hours:minutes:seconds:miliseconds format

Method1: AvgCollisions: 235, AvgDuration: 00:00:00.3561967
Method2: AvgCollisions: 116, AvgDuration: 00:00:00.4042033
Method3: AvgCollisions: 115, AvgDuration: 00:00:04.6037259
Method4: AvgCollisions: 234, AvgDuration: 00:00:09.2195856
Method5: AvgCollisions: 233, AvgDuration: 00:00:09.1788223

I ran the test few times again and it was always circa the same.

Isn’t it strange to you, too? The times aren’t suprise at all that is what I assumed, but the results mean to me, that method2 is the best to generate random numbers, as it is the most random, the fastest and you can set a minimal and maximal generated number. Don’t know how much is the Method2 more predictable over the Method3 as I don’t know how would I test it.

Can someone explain me what am I doning wrong or why methods #4 and #5 don’t have the least collisions and why is the percentage of collisions always the same? Shouldn’t it be random?

EDIT:
Here is Visual Studio 2010 Solution of this test I’ve done: http://bit.ly/nxLODw

  • 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-05-26T04:26:56+00:00Added an answer on May 26, 2026 at 4:26 am

    The only strange behaviour is with method 5.

    In methods 1, 4, you generate a number in the range 0 to int.MaxValue.

    In methods 2, 3 and 5, you generate a number in the range int.MinValue to int.MaxValue.

    So for methods 2 and 3, you have a range that is about twice as large, and they have about half the collisions compared to methods 1, 4. This seems pretty normal to me.

    So why does method 5 produce as many collisions as methods 1 and 4, even though it generates numbers in the larger range? Well, it turns out that the System.Random constructor takes the absolute value of the seed. In other words, it reduces the number of random sequences to half of what it could be. So even though you get numbers from a larger range, you generate them from fewer different sequences.

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

Sidebar

Related Questions

I have found a few libraries to edit MP3 tags (UltraID3Lib is great) but
I have found an interesting issue in windows which allows me to cause the
I have found a few good replies to similar content so far, but never
I am new to .htaccess and I have found a few articles and I've
I have found a few threads in regards to this issue. Most people appear
I always have found the open source space interesting but have never actually participated
I have found a few different questions that pertain to my question, but I'm
I have been doing some research about this and have found a few similar
I've had a look around and I have found a few questions like mine
so I have found a few questions on SO (for example: Automapper mapping list

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.