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

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Knuth is not the place to start. It's the place… May 16, 2026 at 11:32 am
  • Editorial Team
    Editorial Team added an answer The two options are not equivalent. In option 1 there… May 16, 2026 at 11:32 am
  • Editorial Team
    Editorial Team added an answer ^[^<>]*> if you need the corresponding < as well, ^[^<>]*>[^<]*<… May 16, 2026 at 11:32 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

On iPhone.. Why would code such as this cause memory leak? after 2 minutes
Prior to C# generics, everyone would code collections for their business objects by creating
Developing websites are time-consuming. To improve productivity, I would code a prototype to show
As you understand from the title above, I have a datatable and checkboxes in
I am curious what is happening with setValue:forKey: in the code snippet below: is
I would like to write an android app that basically layers an overlay on
I would like to use factory classes and methods to generate GUI components, but
I was trying to create an example for deadlock. I tried the following code.
I went through a period of being interested in how quantum computers work and
I was reading this document: http://www.fadden.com/techmisc/hdc/lesson11.htm In it he stated: Problem is, we don't

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.