Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since 100 / 50 is an integer, this is quite easy. Since 50 / (100 / 50) is an integer, it's even easier.</p> <p>If you didn't quite get that, here is some sample code:</p> <pre><code>int rnd1 = getrnd50(); int rnd2 = getrnd50(); if (rnd1 % 2 == 0) { rnd2 += 50; } return rnd2; </code></pre> <p>Here is an outline:</p> <ul> <li>Two numbers, chosen randomly between 1 and 50, called <strong>a</strong> and <strong>b</strong>.</li> <li>If <strong>a</strong> is even, add 50 to <strong>b</strong>.</li> <li>Return <strong>b</strong>.</li> </ul> <p>You can make this a one-liner if you want:</p> <pre><code>return getrnd50() + getrnd50() % 2 * 50; </code></pre> <p>That's a little too obfuscated though.</p> <p>Edit: I see the question was really asking for a shuffled list, not a sequence of random integers.</p> <p>This can be done by creating a list from 1 to 100, and doing 100 random swaps, like a Fisher-Yates shuffle. I imagine that with a Fisher-Yates shuffle, the absolute minimum number of calls is 93 (given with the formula <code>ceil(log50(100!))</code>), but with a much simpler algorithm you can use 200.</p> <p>The simple algorithm would involve swapping each of the 100 elements with a random element from the 100. The number to choose would be generated from 1-100 with the above generator.</p> <p>For example:</p> <pre><code>for (int i = 0; i &lt; 100; i++) { swap(i, getrnd100() - 1); // - 1 for zero base! } </code></pre> <p>Here is some complete code:</p> <pre><code>int[] result = new int[100]; for (int i = 0; i &lt; 100; i++) { result[i] = i + 1; } for (int i = 0; i &lt; 100; i++) { int j = (getrnd50() + getrnd50() % 2 * 50) - 1; int tmp = result[i]; result[i] = result[j]; result[j] = tmp; } return result; </code></pre> <p>(Disclaimer: I don't know Java, and I haven't tested it.)</p> <p>Best case 200, worst case 200, average case 200.</p>
    singulars
    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. 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