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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:27:01+00:00 2026-05-16T10:27:01+00:00

How would I code a reversible shuffle algorithm in C# which uses a key

  • 0

How would I code a reversible shuffle algorithm in C# which uses a key to shuffle and can be reversed to the original state?

For instance, I have a string: “Hello world”, how can I shuffle it so that later I could be able to reverse the shuffled string back to “Hello world”.

  • 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-16T10:27:01+00:00Added an answer on May 16, 2026 at 10:27 am

    Look at Fisher-Yates shuffle for a way to permute the string based on a key. Feed the key as the seed into a PRNG, use that to generate the random numbers used by the shuffle.

    Now, how to reverse the process? Fisher-Yates works by swapping certain pairs of elements. So to reverse the process you can feed the same key into the same PRNG, then run through the Fisher-Yates algorithm as if you were shuffling an array the size of your string. But actually you don’t move anything, just record the indexes of the elements that would be swapped at each stage.

    Once you’ve done this, run through your list of swaps in reverse, applying them to your shuffled string. The result is the original string.

    So for example, suppose we’ve shuffled the string “hello” using the following swaps (I haven’t used a PRNG here, I rolled dice, but the point about a PRNG is it gives you the same sequence of numbers given the same seed):

    (4,0): "hello" -> "oellh"
    (3,3): "oellh" -> "oellh"
    (2,1): "oellh" -> "olelh"
    (1,0): "olelh" -> "loelh"
    

    So, the shuffled string is “loelh”.

    To deshuffle, I generate the same series of “random” numbers, 0, 3, 1, 0. Then apply the swaps in reverse order:

    (1,0): "loelh" -> "olelh"
    (2,1): "olelh" -> "oellh"
    (3,3): "oellh" -> "oellh"
    (4,0): "oellh" -> "hello"
    

    Success!

    The downside of this of course is that it uses a lot of memory for the deshuffle: an array of indexes as long as your original array of chars. So for truly huge arrays, you might want to choose a PRNG (or anyway a sequence-generation function) that can be stepped either forwards or backwards without having to store all the output. This rules out hash-based cryptographically secure PRNGs, but LFSRs are reversible.

    Btw, why do you want to do this?

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

Sidebar

Related Questions

How you would code an efficient algorithm which can return a social 'distance' between
Can anyone tell me why this code would not be working? $('body').on('test', function() {
can someone give me a hint on how a histogram's pseudo code would look
A piece of code would explain my problem $string = '53 69 cm; 988
Would there be a way to do a zip code lookup based on City/State
Why would the code below throw a io.system.directorynotfound exception? I can't recreate the problem
Would the code bellow cause my code to leak? More specifically, am I responsible
This kind of code would normally work in PHP, but since the scope is
I would like to know if the following code would be a good pattern
I would like to see how this example of existing code would be able

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.