如何实现从一个文本文件中随机提取n行字符?

解决方案 »

  1.   

    Function RandomLine(FileName : String ; Lines : integer) : String;
    var
      StrL : TStringList;
      i : integer;
      Tmpr : Real;
    begin
      Result := '';
      if (Not Fileexists(FileName)) or (Lines<1) then exit;
      StrL := TStringList.Create;
      StrL.LoadFromFile(FileName);
      Tmpr := Int(random*(Strl.Count-Lines));
      if Tmpr<0 then Tmpr := 0;
      for i:= StrToInt(FloatToStr(Tmpr)) to StrToInt(FloatToStr(Tmpr))+Lines do
        Result := Result + StrL.Strings[i] + #13#10;
      Strl.Free;
    end;