Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Shuffle  a stringlist, how to?

Posted on 2013-12-08
2
436 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML text in the body of an email (delphi code) 12 166
Magic Software info 18 138
how to change, disabled button color FMX ? 1 40
Wincontrol not (correctly) drawn 15 40
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

839 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