Forum > General

The necessity of using "delay" between "random" calling [SOLVED]

(1/2) > >>

pascal111:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
I think you should call randomize only once, not every time. Put it somewhere in the beginning of program.

pascal111:

--- Quote from: dseligo on May 04, 2021, 05:54:38 pm ---I think you should call randomize only once, not every time. Put it somewhere in the beginning of program.

--- End quote ---

I called it once here in this next procedure, or you think its position isn't an appropriate one:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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:
Move randomize from card_generating to FormCreate.

dseligo:
And remove delays.

Navigation

[0] Message Index

[#] Next page

Go to full version