(page requirements)

Shuffle strings...

So you don't like ordered / sorted string lists? Well, whatever the reason you have behind shuffling a string list, the following ShuffleStrings() function will do it for you.

All you have to do is pass the string list (type of TStrings) you want to shuffle and the intensity. "Intensity" is just a number between 1 and the number of strings in your string list. ShuffleStrings() function will use this value to find out how many times it should shuffle. If you're not sure about this number, simply pass 0, and ShuffleStrings() will use the count of strings in your string list.

procedure ShuffleStrings(
  sl : TStrings;
  nIntensity : integer );
var
  n1, n2, n3 : integer;
  s1         : string;        
begin
  if(0 = nIntensity)then
  begin
    nIntensity := sl.Count;
  end else
  if(nIntensity > sl.Count)then
  begin
    nIntensity := sl.Count;
  end;

  Randomize;
  
  for n1 := 1 to nIntensity do
  begin
    n2 := Random( nIntensity );
    n3 := Random( nIntensity );

    s1             := sl.Strings[n3];
    sl.Strings[n3] := sl.Strings[n2];
    sl.Strings[n2] := s1;
  end;
end;

Example call:

(assuming that you want to shuffle items in your ListBox named listbox1)

ShuffleStrings( ListBox1.Items, 0 );

 
 
Applicable Keywords : Delphi, Delphi 1.x, Delphi 2.x, Functions
 
 
 
Copyright © 2009 Chami.com. All Rights Reserved. | Advertise | Created in HTML Kit editor