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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:36:02+00:00 2026-06-16T07:36:02+00:00

Well I want to generate pseudo-random bits for a mathematical problem. The Bit size

  • 0

Well I want to generate pseudo-random bits for a mathematical problem. The Bit size is user fed. But unlike normal problems there’s a small twist here. The mathematical problem also gives valid results if the bits begin with 0’s and literally match the size input by user.
Example: For User Input 4, all the following are valid outputs:

-> 0000
-> 0001
-> 1000
-> 0100

etc.
I am using this function to generate the numbers now:

int randomChoice = choice.Next(1 << (input - 1), (1 << input) - 1);
bitSize = randomChoice;

Is there any other way of random bit generation that may begin with 0’s as long as it follows the literal bit length in C# without going into complex array manipulation?

  • 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-16T07:36:03+00:00Added an answer on June 16, 2026 at 7:36 am

    The following line:

    int randomChoice = choice.Next(1 << (input - 1), (1 << input) - 1)
    

    for input = 4 is equivalent to:

    int randomChoice = choice.Next(8, 15);
    

    Which will per MSDN documentation pick one of the following values: 8, 9, 10, 11, 12, 13, 14 – randomly. Notice that the value 15 will not be picked as the documentation states clearly that the lower bound (8) is )inclusive_ while the upper bound (15) is exclusive.

    Those values in binary are:

    8 : 1000
    9 : 1001
    10: 1010
    11: 1011
    12: 1100
    13: 1101
    14: 1110
    

    I believe what @Jon Skeet was saying is that you are generating random numbers that always have the MSB (left-most bit) set. And are thus missing out on all values that fit in 4 bits that would start with 0 (have the MSB unset).

    I would also point out that you should probably include value 15 here, since that also fits in the 4 bits. So, with the two modifications, the line of code should read:

    int randomChoice = choice.Next(0, (1 << input));
    

    That would generate all values from 0 to 15, inclusive:

    0 : 0000
    1 : 0001
    2 : 0010
    3 : 0011
        ...
    13: 1101
    14: 1110
    15: 1111
    

    Is that what you’re looking for?

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

Sidebar

Related Questions

I want to generate eight bit (uint8_t) random numbers so that I exclude a
I would like a function that can generate a pseudo-random sequence of values, but
I want to generate the xml file that holds its schema as well as
I can generate a client from http://localhost:8080/service?wsdl just fine, but now I want to
Background I want to enable right-to-left locales as well as left-to-right, but I only
Hope the title was OK. My problem is that I want to generate an
I want to generate programtically a jar file in java. This works well when
For an ecommerce site I want to generate a random coupon code that looks
well I want to debug some script with Firebug, (cause I can't see anything
Well I want to render a self closing tag say <img> tag like this

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.