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

  • SEARCH
  • Home
  • 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 8210995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:16:32+00:00 2026-06-07T10:16:32+00:00

If I wanted to generate a random number for all possible numbers an Int32

  • 0

If I wanted to generate a random number for all possible numbers an Int32 could contain would the following code be a reasonable way of doing so? Is there any reason why it may not be a good idea? (ie. a uniform distribution at least as good as Random.Next() itself anyway)

    public static int NextInt(Random Rnd) //-2,147,483,648 to 2,147,483,647
    {
         int AnInt;
         AnInt = Rnd.Next(System.Int32.MinValue, System.Int32.MaxValue);
         AnInt += Rnd.Next(2);
         return AnInt;
    }
  • 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-07T10:16:34+00:00Added an answer on June 7, 2026 at 10:16 am

    Your proposed solution will slightly skew the distribution. The minValue and maxValue will occur less frequently than the interior values. As an example, assume that int has a MinValue of -2 and a MaxValue of 1. Here are the possible initial values, with each followed by the resulting values after the Random(2):

    -2: -2 -1
    -1: -1  0
     0:  0  1
    

    half of the negative -2 values will get modified up to -1, and only half of 0 will get modified up to 1. So the values -2 and 1 will occur less frequently than -1 and 0.

    Damien’s solution is good. Another choice would be:

    if (Random(2) == 0) {
        return Random(int.MinValue, 0);
    } else {
        return 1 + Random(-1, int.MaxValue);
    }
    

    another solution, similar to Damiens approach, and faster than the previous one would be

     int i = r.Next(ushort.MinValue, ushort.MaxValue + 1) << 16;
     i |= r.Next(ushort.MinValue, ushort.MaxValue + 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to create a simple program which would generate random numbers between 0
I wanted to generate a random number 32 characters in length from a specified
I just wanted to know how do I generate random number using J2ME CLDC
Is it possible to create a random number from a selection of numbers in
I wanted to generate a list view using below code. But after running this
If you wanted to generate a pseudorandom alphanumeric string using T-SQL, how would you
I wanted to know if it's possible to derive a method to generate a
I am trying to generate some code using CodeDom. I wanted to know if
Wanted to know if there was a way one could query shelveset details from
I wanted to generate the following using XmlSerializer : <atom:link href=http://dallas.example.com/rss.xml rel=self type=application/rss+xml />

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.