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