Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type
- TRandomValues = array[1..4] of Integer;
- procedure GetRandomValues(out Values: TRandomValues);
- var
- IntSum: Integer;
- begin
- // initialize a helper variable for calculating sum of the generated numbers
- IntSum := 0;
- // in the first step just generate a number in the range of 2 to 7 and store
- // it to the first integer element
- Values[1] := RandomRange(2, 7);
- // and increment the sum value
- IntSum := IntSum + Values[1];
- // as the next step we need to generate number, but here we need also say in
- // which range by the following rules to ensure we ever reach 22 (consider, if
- // the 1st number was e.g. 3, then you can't generate the second number smaller
- // than 5 because then even if the next two numbers would be max, you would get
- // e.g. only 3 + 4 + 7 + 7 = 21, so just use this rule:
- // Values[1] Values[2]
- // 2 6..7
- // 3 5..7
- // 4 4..7
- // 5 3..7
- // 6..7 2..7
- Values[2] := RandomRange(Max(2, 8 - Values[1]), 7);
- // and increment the sum value
- IntSum := IntSum + Values[2];
- // if the third step we need to generate a value in the range of 15 to 20 since
- // the fourth number can be still in the range of 2 to 7 which means that the sum
- // after this step must be from 22-7 to 22-2 which is 15 to 20, so let's generate
- // a number which will fit into this sum
- Values[3] := RandomRange(Max(2, Min(7, 15 - IntSum)), Max(2, Min(7, 20 - IntSum)));
- // and for the last number let's just take 22 and subtract the sum of all previous
- // numbers
- Values[4] := 22 - (IntSum + Values[3]);
- end;
Advertisement
Add Comment
Please, Sign In to add comment