TLama

Untitled

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