TLama

Untitled

Oct 19th, 2013
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.89 KB | None | 0 0
  1. // assuming that Randomize was called somewhere at the application initialization
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   I: Integer;
  6.   IntSum: Integer;
  7.   IntArray: array[1..4] of Integer;
  8. begin
  9.   // initialize a helper variable which counts the sum of the first 3 random integers
  10.   IntSum := 0;
  11.   // iterate 3 times to generate first 3 numbers
  12.   for I := 1 to 3 do
  13.   begin
  14.     // generate a random number in the range of 2 to 7
  15.     IntArray[I] := Random(6) + 2;
  16.     // and increment the helper sum variable by the just generated number
  17.     IntSum := IntSum + IntArray[I];
  18.   end;
  19.   // and fill the last number, which must be 22 minus the sum of the previous 3 numbers,
  20.   // which we have in the helper sum variable (so we don't need to calculate it anymore)
  21.   IntArray[4] := 22 - IntSum;
  22.   // now you should have in the IntArray array values between 2-7 whose sum is 22...
  23. end;
Advertisement
Add Comment
Please, Sign In to add comment