How to randomise lazarus / 9-Editor ---------------------------------------------------------------------- zeljkosh · 2015-02-08 19:39 Hi there guys, im kinda new to Lazarus and i want to make a very tiny app that will randomly pick one movie out of five, so basicly i put this in to my form : 5 "Tedit"s named Movie1,Movie2...Movie5 i made a button "random" and i made an label that should display the result.... but i have no idea how to make this work, i was reading about it a bit here http://www.delphibasics.co.uk/RTL.asp?Name=Random , but i dont know how to apply that on my issue. Any help is welcomed! ---------------------------------------------------------------------- jwdietrich · 2015-02-08 20:21 You should call randomize once in the beginning of your program or in the initialization section of your unit. This sets the seed of Free Pascal's random number generator. If you omit the call to randomize you will get pseudo-random numbers, too, but their sequence will be the same every time the program is launched. ---------------------------------------------------------------------- howardpc · 2015-02-08 21:07 You could try this: start a new Lazarus project. Double-click the form to create an OnCreate event handler, and complete the unit as follows: Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   SysUtils, Forms, ExtCtrls, StdCtrls; type   TEditArray = array[0..4] of TLabeledEdit;   { TForm1 }   TForm1 = class(TForm)     procedure FormCreate(Sender: TObject);   private     EdArray: TEditArray;     BChooseMovie: TButton;     LMovieChoice: TLabel;     function AllEditsFilled: boolean;     procedure BRandomClick(Sender: TObject);     procedure EditEditingDone(Sender: TObject);   end; var   Form1: TForm1; implementation uses math; {$R *.lfm} { TForm1 } procedure TForm1.BRandomClick(Sender: TObject); var   j: integer; begin   j:=RandomRange(0,High(TEditArray)+1);   LMovieChoice.Caption:=EdArray[j].Text; end; procedure TForm1.EditEditingDone(Sender: TObject); begin   BChooseMovie.Enabled:=AllEditsFilled; end; procedure TForm1.FormCreate(Sender: TObject); var   i: integer; begin   for i:=Low(TEditArray) to High(TEditArray) do     begin       EdArray[i]:=TLabeledEdit.Create(Self);       EdArray[i].SetBounds(70, i*30+10, 150, 18);       EdArray[i].LabelPosition:=lpLeft;       EdArray[i].EditLabel.Caption:=Format('Movie%d: ',[i+1]);       EdArray[i].OnEditingDone:=@EditEditingDone;       EdArray[i].Parent:=Self;     end;   BChooseMovie:=TButton.Create(Self);   BChooseMovie.AutoSize:=True;   BChooseMovie.Caption:='Show random movie title from above edits';   BChooseMovie.Enabled:=AllEditsFilled;   BChooseMovie.Left:=10;   BChooseMovie.Top:=160;   BChooseMovie.OnClick:=@BRandomClick;   BChooseMovie.Parent:=Self;   LMovieChoice:=TLabel.Create(Self);   LMovieChoice.SetBounds(10, 200, 150, 18);   LMovieChoice.Parent:=Self;   Randomize; end; function TForm1.AllEditsFilled: boolean; var   i: integer; begin   Result:=True;   for i:=Low(TEditArray) to High(TEditArray) do     if (EdArray[i].Caption = '') then       Result:=False; end; end.