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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:26:16+00:00 2026-06-15T17:26:16+00:00

I need to generate a random number, but it needs to be selected from

  • 0

I need to generate a random number, but it needs to be selected from the set of binary numbers with equal numbers of set bits. E.g. choose a random byte value with exactly 2 bits set…

00000000 - no
00000001 - no
00000010 - no
00000011 - YES
00000100 - no
00000101 - YES
00000110 - YES
...

=> Set of possible numbers 3, 5, 6...

Note that this is a simplified set of numbers. Think more along the lines of ‘Choose a random 64-bit number with exactly 40 bits set’. Each number from the set must be equally likely to arise.

  • 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-15T17:26:16+00:00Added an answer on June 15, 2026 at 5:26 pm

    Do a random selection from the set of all bit positions, then set those bits.

    Example in Python:

    def random_bits(word_size, bit_count):
        number = 0
        for bit in random.sample(range(word_size), bit_count):
            number |= 1 << bit
        return number
    

    Results of running the above 10 times:

    0xb1f69da5cb867efbL
    0xfceff3c3e16ea92dL
    0xecaea89655befe77L
    0xbf7d57a9b62f338bL
    0x8cd1fee76f2c69f7L
    0x8563bfc6d9df32dfL
    0xdf0cdaebf0177e5fL
    0xf7ab75fe3e2d11c7L
    0x97f9f1cbb1f9e2f8L
    0x7f7f075de5b73362L
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate a lot of random numbers. I've tried using random.random but
I need to generate a random port number between 2000-65000 from a shell script.
I need to generate a random number. But the twist is that the %
I need to generate about 5,000 random numbers in my script, but the CPU
A specific example I need to generate a random number between 0 and 2,
Like my question, i need to generate random numbers that have identical pairs between
I need to generate an random array from a bigger array of int without
I need to generate a series of N random binary variables with a given
I need to generate a controlled sequence of pseudo-random numbers, given an initial parameter.
I need to get a random number in C#, within (-15, 15) but without

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.