人物洗牌机 [英] Character Shuffler

查看:93
本文介绍了人物洗牌机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有一种方法(使用ASP.NET C#)改组字符串的内容,但是仍然能够单击另一个按钮并将其取消混编回其原始内容,而无需保存

I was just wondering if there's a way (using ASP.NET C#) to "shuffle" up the contents of a string, but still be able to click another button and "UNshuffle" it back to its original content withOUT saving the original content?

谢谢:)

示例:

"This is not shuffled."

"isuo .tffsnl iTh shed"

...And then I click the "UNShuffle" button and it becomes normal again:

"This is not shuffled."


推荐答案

那么,您需要保存 。一个简单的想法:

Well, you'd need to save something. One simple idea:


  • 使用随机数生成器生成随机种子。

  • 创建一个 Random 的新实例,用于使用该种子进行改组

  • 使用经过修改的 Fisher-Yates随机播放

  • 保持种子状态

  • Use a random number generator to generate a random seed.
  • Create a new instance of Random to use for shuffling using that seed
  • Shuffle with a modified Fisher-Yates shuffle
  • Keep hold of the seed

然后,改组是可逆的-当然要付出一点努力。 (我可能会以相同的方式将数字0 ...(n-1)随机播放,然后以这种方式反向映射字符。)

The shuffle is then reversible - admittedly with a bit of effort. (I'd probably shuffle the numbers 0...(n-1) in the same way, and then reverse map the characters that way.)

棘手的地方是您确实需要种子-它有点像存储的密码哈希中的盐。您必须有一些额外的信息来说明如何对其进行改组,否则您将不知道 abc是来自 bac还是 cab。

The tricky bit is that you do need the seed - it's a bit like a salt in a stored password hash. You've got to have some extra bit of information to say how it was shuffled otherwise you won't know whether "abc" came from "bac" or "cab" for example.

这篇关于人物洗牌机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆