随机化StringList [英] Randomize StringList

查看:121
本文介绍了随机化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 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屋!

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