• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 508
  • Last Modified:

Shuffle Lines In A Memo

I am writing an application to play Mp3 Files.  I am using the memo for my play list.  I want to shuffle the songs in the memo but I don't want to use random, because I don't want the same song to play twice.  Does anyone have some advice on how to do this.
0
brettawv
Asked:
brettawv
1 Solution
 
men xinCEOCommented:
like winamp,initiate the list at the first time(use a array and random)
Like this.

public var i:integer=0;

oncreate:
shuffle();

after a song completed:
if i=songscount then begin
  i:=0;
  shuffle();
end else
  inc(i);

shuffle function:
random...

Sorry for my POOR English.

menxin
0
 
edeyCommented:
I think what menxin is alluding to is to create a randomized list by modifying the original.  For example, "swap" two lines in you TStringList, & repeat a large number of times like so:

procedure swap(t : TStringList);
var
 ix,src,dst : integer;
 s : string;
begin
 for ix := 0 to 100 do
 begin
  src := random(t.count);
  dst := random(t.count);
  s := t[dst];
  t[dst] := t[src];
  t[src] := s;
 end;
end;


One should note that the above is untested (and could prob. be considered pseudo code :)


GL
Mike
0
 
Fatman121898Commented:
In order to avoid the same playlist order every time you start it, you should use Randomize procedure before  using Random function. The code proposed by edey should be modified in this way:

procedure swap(var t : TStringList);
var
 ix,src,dst : integer;
 s : string;
begin
 Randomize;
 for ix := 0 to 100 do
 begin
  src := random(t.count);
  dst := random(t.count);
  s := t[dst];
  t[dst] := t[src];
  t[src] := s;
 end;
end;


Jo.
0
 
DragonSlayerCommented:
You should only call Randomize ONCE.
0
 
Fatman121898Commented:
Did I call it more than once? ;-)

Jo.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now