随机化StringList [英] Randomize StringList
问题描述
如何类似于此在线工具的工作原理,如何在StringList中随机化字符串。如果有人熟悉它,请检查以下内容: http://textmechanic.co/Randomize-List.html
How can I randomize the Strings in the StringList similarly how this online tool works. If anyone is familiar with it, check this: http://textmechanic.co/Randomize-List.html
推荐答案
执行随机播放的一种常见算法是 Fisher-Yates 随机播放。这样会生成均匀分布的排列。
One common algorithm to perform a shuffle is the Fisher-Yates shuffle. This generates uniformly distributed permutations.
要在Delphi TStrings
对象上实现,可以使用以下方法:
To implement on a Delphi TStrings
object you can use this:
procedure Shuffle(Strings: TStrings);
var
i: Integer;
begin
for i := Strings.Count-1 downto 1 do
Strings.Exchange(i, Random(i+1));
end;
现在,虽然理论上这会产生均匀分布的排列,但实际性能在很大程度上取决于随机数生成器。这在Knuth的计算机编程艺术,第2卷,第3.4.2节,算法P中进行了讨论。
Now, whilst in theory this will generate uniformly distributed permutations, the actual performance depends heavily on the quality of the random number generator. This is discussed in Knuth's Art of Computer Programming, volume 2, section 3.4.2, Algorithm P.
更多阅读内容:
- Fisher-Yates随机播放 (维基百科)
- Jeff Attwood的两篇有关改组的博客文章:改组和Naïveté的危险
- Fisher-Yates改组背后的直觉(Eli Bendersky)
- 计算机编程艺术,Donald Knuth,第2卷,第3.4.2节
- 嘘呕吐(维基百科)
- Fisher-Yates shuffle (Wikipedia)
- Jeff Attwood's two blog articles on shuffling: Shuffling and The Danger of Naïveté
- The intuition behind Fisher-Yates shuffling (Eli Bendersky)
- Art of Computer Programming, Donald Knuth, volume 2, section 3.4.2
- Shuffling (Wikipedia)
这篇关于随机化StringList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!