Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate random numbers from 1-100 from a generator of 1-50
    primarykey
    data
    text
    <p>in a recent interview i was asked the following question: </p> <blockquote> <p>Print random numbers from 1-100 using the given <code>getrnd50()</code> method which generates the random numbers from 1-50. Each random number should be printed only once and in random order. Use of no other random number generator is allowed and i was not allowed to change the definition of <code>getrnd50()</code>.</p> </blockquote> <p>I came up with the following code which gives the correct output.</p> <pre><code>import java.util.Random; public class Test { public static void main(String[] args) { int[] rs = new int[100]; int count = 0; int k; while (count != 100) { // I decided to simply multiply the result of `getrnd50()` by 2. // But since that would generate only even numbers, k = getrnd50() * 2; // I decided to randomly subtract 1 from the numbers. // Which i accomlished as follows. if (getrnd50() &lt;= 25) // 25 is to half the possibilities. k--; // Every number is to be stored in its own unique location // in the array `rs`, the location is `(number-1)`. // Every time a number is generated it is checked whether it has // already been generated. If not, it is saved in its position, printed and // `count` is incremented. if (rs[k-1] == 0) { rs[k-1] = k; count++; System.out.print(k + " "); } } } // This is the method and i am not supposed to touch it. static int getrnd50() { Random rand = new Random(); return (1 + rand.nextInt(50)); } } </code></pre> <p>While it was accepted in that round, in the next round the interviewer tells me that <code>getrnd50()</code> is a costly method and even in best case scenario i have to call it twice for every number generated. i.e. 200 times for 1-100. In worst case scenario it would be infinity and tens of thousand in average case. He asks me to optimize the code so as to <strong>significantly</strong> improve the average case.</p> <p>He gave me a hint when i expressed my inability to do it, he said: </p> <blockquote> <p>To consider the number of numbers generated while generating a new number. For ex. if <code>count</code> becomes 99 i don't have to call <code>getrnd50()</code> I can simply find the remaining number and print it.</p> </blockquote> <p>While i understood his drift i had no idea how it would help me, so obviously i got rejected. Now i am curious to know the answer. Help me! Thanx in advance!</p> <p><strong>Note:</strong> if anyone is feeling lazy to to write a lengthy code just point out the numer generation part, the rest is easy. Also we are not bound to follow the hint.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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