Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several ways of defining what "not biased" means in this case. I assume that what you are looking for is that every possible subtraction problem from the allowed problem space is chosen with equal probability. Quick and dirty approach:</p> <ol> <li>Pick random x in [x_min, x_max]</li> <li>Pick random y in [y_min, y_max]</li> <li>If x - y &lt; answer_min, discard <strong>both x and y</strong> and start over.</li> </ol> <p>Note the bold part. If you discard only y and keep the x, your problems will have an uniform distribution in x, not in the entire problem space. You need to ensure that for every valid x there is at least one valid y - this is not the case for your original choice of ranges, as we'll see later.</p> <p>Now the long, proper approach. First we need to find out the actual size of the problem space.</p> <p>The allowed set of subtrahends is determined by the minuend:</p> <pre><code>x in [21, 99] y in [11, x-10] </code></pre> <p>or using symbolic constants:</p> <pre><code>x in [x_min, x_max] y in [y_min, x - answer_min] </code></pre> <p>We can rewrite that as</p> <pre><code>x in [21, 99] y = 11 + a a in [0, x-21] </code></pre> <p>or again using symbolic constants</p> <pre><code>x in [x_min, x_max] y = y_min + a a in [0, x - (answer_min + y_min)]. </code></pre> <p>From this, we see that valid problems exist only for x >= (answer_min + y_min), and for a given x there are x - (answer_min + y_min) + 1 possible subtrahents.</p> <p>Now we assume that x_max does not impose any further constraints, e.g. that answer_min + y_min >= 0:</p> <pre><code>x in [21, 99], number of problems: (99 - 21 + 1) * (1 + 78+1) / 2 x in [x_min, x_max], number of problems: (x_max - x_min + 1) * (1 + x_max - (answer_min + y_min) + 1) / 2 </code></pre> <p>The above is obtained using the formula for the sum of an arithmetic sequence. Therefore, you need to pick a random number in the range [1, 4740]. To transform this number into a subtraction problem, we need to define a mapping between the problem space and the integers. An example mapping is as follows:</p> <ul> <li>1 &lt;=> x = 21, y = 11</li> <li>2 &lt;=> x = 22, y = 12</li> <li>3 &lt;=> x = 22, y = 11</li> <li>4 &lt;=> x = 23, y = 13</li> <li>5 &lt;=> x = 23, y = 12</li> <li>6 &lt;=> x = 23, y = 11</li> </ul> <p>and so on. Notice that x jumps by 1 when a triangular number is exceeded. To compute x and y from the random number r, find the lowest triangular number t greater than or equal to r, preferably by searching in a precomputed table; write this number as q*(q+1)/2. Then x = x_min + q-1 and y = y_min + t - r.</p> <p>Complete program:</p> <pre><code>import random x_min, x_max = (21, 99) y_min = 11 answer_min = 10 triangles = [ (q*(q+1)/2, q) for q in range(1, x_max-x_min+2) ] upper = (x_max-x_min+1) * (1 + x_max - (answer_min + y_min) + 1) / 2 for i in range(0, 20): r = 1 + random.randrange(0, upper) (t, q) = next(a for a in triangles if a[0] &gt;= r) x = x_min + q - 1 y = y_min + t - r print "%d - %d = ?" % (x, y) </code></pre> <p>Note that for a majority of problems (around 75%), x will be above 60. This is correct, because for low values of the minuend there are fewer allowed values of the subtrahend.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload