The necessity of using "delay" between "random" calling [SOLVED] lazarus / 62-General ---------------------------------------------------------------------- pascal111 · 2021-05-04 15:13 This is a small program that generates two playing cards and making comparison between 'em, the problem is that I have to use "delay" between the generating of the two cards or I'll must get same two same cards in all tries. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);  var  s:string;  e:card_comparison;  begin   card_a^.card_generating;  delay(1000);  card_b^.card_generating;   e:= cards_equal(card_a, card_b);  str(e,s);   showmessage('Card A is ' + card_a^.name+#10#13+             'Card B is ' + card_b^.name+#10#13+               'Equality is ' + s); end;   in Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+}{$modeSwitch advancedRecords} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,  Crt; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private   public   end;   card_pattern = (Club, Diamond, Heart, Spade);   playing_card = record    value:2..14;    name:string[20];    pattern:card_pattern;    procedure card_generating;  end;   pcard = ^playing_card;   card_comparison = (value, pattern, both, none); function cards_equal(card_x, card_y:pcard):card_comparison; var  Form1: TForm1;  card_a,  card_b:pcard;   implementation {$R *.lfm} { TForm1 } procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin   end; procedure TForm1.Button1Click(Sender: TObject);  var  s:string;  e:card_comparison;  begin   card_a^.card_generating;  delay(1000);  card_b^.card_generating;   e:= cards_equal(card_a, card_b);  str(e,s);   showmessage('Card A is ' + card_a^.name+#10#13+               'Card B is ' + card_b^.name+#10#13+               'Equality is ' + s); end; procedure TForm1.FormCreate(Sender: TObject); begin   new(card_a);  new(card_b); end; procedure TForm1.FormDestroy(Sender: TObject);begin   dispose(card_a);  dispose(card_b); end; procedure playing_card.card_generating; var  x:2..14;  y:ord(low(card_pattern))..ord(high(card_pattern));  s1,s2:string[8]; begin   randomize;  delay(10);   x:=random(12+1)+2;  value:=x;   case x of  11:s1:='JACK';  12:s1:='QUEEN';  13:s1:='KING';  14:s1:='ACE';  else    str(x,s1);  end;   y:=random(ord(high(card_pattern))+1);  pattern:=card_pattern(y);   str(pattern,s2);   name:=s1+' '+s2; end; function cards_equal(card_x, card_y:pcard):card_comparison; var  temp_e:card_comparison; begin   if (card_x^.value = card_y^.value) and  (card_x^.pattern <> card_y^.pattern) then   temp_e := value;   if (card_x^.value <> card_y^.value) and  (card_x^.pattern = card_y^.pattern) then   temp_e := pattern;   if (card_x^.value = card_y^.value) and  (card_x^.pattern = card_y^.pattern) then   temp_e := both;   if (card_x^.value <> card_y^.value) and  (card_x^.pattern <> card_y^.pattern) then   temp_e := none;   cards_equal:= temp_e; end; end. ---------------------------------------------------------------------- dseligo · 2021-05-04 15:54 I think you should call randomize only once, not every time. Put it somewhere in the beginning of program. ---------------------------------------------------------------------- pascal111 · 2021-05-04 15:59 Quote from: dseligo on May 04, 2021, 05:54:38 pmI think you should call randomize only once, not every time. Put it somewhere in the beginning of program. I called it once here in this next procedure, or you think its position isn't an appropriate one: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  procedure playing_card.card_generating; var  x:2..14;  y:ord(low(card_pattern))..ord(high(card_pattern));  s1,s2:string[8]; begin   randomize;  delay(10);   x:=random(12+1)+2;  value:=x;   case x of  11:s1:='JACK';  12:s1:='QUEEN';  13:s1:='KING';  14:s1:='ACE';  else    str(x,s1);  end;   y:=random(ord(high(card_pattern))+1);  pattern:=card_pattern(y);   str(pattern,s2);   name:=s1+' '+s2; end; ---------------------------------------------------------------------- dseligo · 2021-05-04 16:04 Move randomize from card_generating to FormCreate. ---------------------------------------------------------------------- dseligo · 2021-05-04 16:05 And remove delays. ---------------------------------------------------------------------- Bart · 2021-05-04 16:13 Randomize initializes the global variable RandSeed. It uses a.o. the current system time to calculate it. If you call randomize twice with only nanoseconds between it, the system time will be the same in both calls. Therefore RandSeed will be the same after both calls. RandSeed then is used to calculate the result of Random(). So, both calls to Random() will have the same result. With the delay, the system time is different after each call, so Random() will be different too. As others explained: you should call Randomize only once (in the entire program). The above tries to explain why this is the case. Bart ---------------------------------------------------------------------- pascal111 · 2021-05-04 16:17 I moved "randomize" to "FormCreate", and removed "delays", as @dseligo and @Bart suggested and explained and the program works now appropriately. Thanks everyone!