如何随机化字符序列?
我想编写一个函数来随机化字母字符序列的顺序。例如,序列:
ABCDEFG。 。 。
...可能会更改为:
ZLTAP ...
...如果再次传递给同一函数可能会导致:
HREIC ....
有什么建议吗?
I want to write a function which randomizes the order of a sequence of alphabetic characters. For example, the sequence:
A B C D E F G . . .
...might be changed to:
Z L T A P ...
...which, if passed to the same function again could result in:
H R E I C ....
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的意思是随机字母表?几天前我用 PHP 写了类似的东西。逻辑如下:
结果是随机打乱的一组字符,以最小的 CPU 负载创建(如果字符串有 26 个字符,则内部循环只需要 26 次迭代)。
You mean randomise the alphabet? I wrote something similar in PHP a few days ago. The logic was the following:
The result is a randomly shuffled set of characters, created with minimal CPU load (if the string has 26 characters, the inner loop only needs 26 iterations).
这听起来像是家庭作业,但无论如何:
http://stanford.edu/~blp /writings/clc/shuffle.html
This sounds like homework, but either way:
http://stanford.edu/~blp/writings/clc/shuffle.html
查看 Fisher-Yates shuffle 算法,特别是 现代版本。
Have a look at the Fisher-Yates shuffle algorithm, and in particular the modern version of it.