Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shuffle  a stringlist, how to?

Posted on 2013-12-08
2
Medium Priority
?
506 Views
Last Modified: 2013-12-09
Hello Guys,

is there any function in Delphi where I can shuffle a stringlist? If no, do you have one that could me send?

I Use Delphi XE4

Consider my SL with this:

sl.Add('one');
sl.Add('two');
sl.Add('three');
sl.Add('four');
sl.Add('five');
sl.Add('six');
sl.Add('seven');
sl.Add('eight');
sl.Add('nine');
sl.Add('ten');

How can I shuffle it?

Thanks
Alex
0
Comment
Question by:hidrau
2 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 39704614
Try this:

 procedure Shuffle(list : TStringList) ;
 var
   randomIndex: integer;
   cnt: integer;
 begin
   Randomize;
   for cnt := 0 to list.Count -1 do
   begin
     randomIndex := Random(-cnt + list.Count) ;
     list.Exchange(cnt, cnt + randomIndex) ;
   end;
 end;

Open in new window

0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 39706617
Thanks very much.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question