Write an efficient algorithm to print the following two outputs
You are given a pre-defined function named getrand100() which returns
an integer which is one random number from 1-100. You can call this
function as many times as you want but beware that this function is
quite resource intensive. You cannot use any other random generator.
You canNOT change the definition of getrand100().
int getrand100(){
Random rand = new Random();
return (1+rand.nextInt(100));
}
- Output1: Print numbers 1-20 in random order. (Not 20 random numbers)
- Output2: Print numbers 1-200 in random order. (Not 200 random numbers)
Note:
- i. Every number should be printed exactly once.
- ii. There should be no pattern in the numbers listing. List should be completely random
i.e., all numbers have equal probability appearing at any place. - iii. You may call getrand100() any number of time to get randomnumber
from 1 to 100. - iv. You cannot use any other random generator
function except getrand100().
The idea is to use the Random generator given to compute the required random numbers.
1) For random numbers 1-20, just divide the 100 numbers equally to represent 1 to 20.
2) To generate 1-200, find the even numbers from 1 to 200 and then add (-1 or 0) to it to get all the numbers from 1 to 200.