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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:13:55+00:00 2026-06-11T06:13:55+00:00

I have a use case where I need to scramble an input in such

  • 0

I have a use case where I need to scramble an input in such a way that:

  1. Each specific input always maps to a specific pseudo-random output.
  2. The output must shuffle the input sufficiently so that an incrementing input maps to a pseudo-random output.

For example, if the input is 64 bits, there must be exactly 2^64 unique outputs, and these must break incrementing inputs as much as possible (arbitrary requirement).

I will code this in C#, but can translate from Java or C, so long as there are not SIMD intrinsics. What I am looking for is some already existing code, rather than reinventing the wheel.

I have looked on Google, but haven’t found anything that does a 1:1 mapping.

  • 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-11T06:13:56+00:00Added an answer on June 11, 2026 at 6:13 am

    This seems to work fairly well:

    const long multiplier = 6364136223846793005;
    const long mulinv_multiplier = -4568919932995229531;
    const long offset = 1442695040888963407;
    
    static long Forward(long x)
    {
        return x * multiplier + offset;
    }
    
    static long Reverse(long x)
    {
        return (x - offset) * mulinv_multiplier;
    }
    

    You can change the constants to whatever as long as multiplier is odd and mulinv_multiplier is the modular multiplicative inverse (see wiki:modular multiplicative inverse or Hackers Delight 10-15 Exact Division by Constants) of multiplier (modulo 2^64, obviously – and that’s why multiplier has to be odd, otherwise it has no inverse).

    The offset can be anything, but make it relatively prime with 2^64 just to be on the safe side.

    These specific constants come from Knuths linear congruential generator.

    There’s one small thing: it puts the complement of the LSB of the input in the LSB of the result. If that’s a problem, you could just rotate it by any nonzero amount.


    For 32 bits, the constants can be multiplier = 0x4c957f2d, offset = 0xf767814f, mulinv_multiplier = 0x329e28a5.

    For 64 bits, multiplier = 12790229573962758597, mulinv_multiplier = 16500474117902441741 may work better.


    Or, you could use a CRC, which is reversible for this use (ie the input is the same size as the CRC) for CRC64 it requires some modifications of course.

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

Sidebar

Related Questions

We have the use case that we need to output text that is a
I have a use case where I need to call a (non-static) method in
I have a use case where I only need to store certain fields to
I have a strong use case for pre-allocating all the memory I need upfront
I have this use case of an xml file with input like Input: <abc
I am pretty new to android. I have a use case where I need
In Zend Framework, I have a common use case where I need to propagate
I have the following use case: I need to process a big number of
I have a use case where I need to add information about the user
I'm new to design patterns, I have a use case where I need to

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.