Write a methodA that returns a random number between 0 to 3, Use the same methodA to return a random number between 0 and 7. (do not use Random function) .This is a question which comes under uniform distribution principle.

Assume i write a methodA and i have to dynamically pass either 0 or 1 or 2 or 3 to the same methodA,this methodA should display a number between 0 to 7

Rand3(int input) should return dynamically either 0 or 1 or 2 or 3
The same Rand3(int input) should then return any value between 0 to 7.

Language is Core Java 1.5
Solution in Core Java Needed

Step1:
Rand3(int input){
//how do i return either 0,1,2,3 dynamically to Rand3() function
}
Step2:pass either 0,1,2,3 to same Rand3 function
Rand3(either 0,1,2,3)
// generate a number between 0 and 7
}

PHein.This was the question asked in a recent interview which i failed to answer and i searched google for answers but im not getting any solutions.
if you can help me with this.it would be fine.
The interviewer told me not to use Math.random() function to generate the number and use your own logic so i do not know the logic for dynamically generating numbers,as i have indicated in the code snippet

Kayaman wrote:
But I guess it doesn't matter, you clearly don't know enough Java to be hired.

It has little to do with Java, this is a generic trivia question that is not language-specific. How do you generate random numbers without using a built-in random number generator?

I wouldn't be able to solve that on the spot either without at least pen and paper and a good amount of time to think about it (my initial thought is a set of numbers and some sort of shuffle algorithm), but then again when they would start to pelt stupid trivia questions at me without asking questions that really test someone, I would get up, shake hands and walk away. A job interview is a test that goes both ways and to me the interviewer, representing the entire company, would have failed miserably.

One (poor) way of solving this is to get the current system time in milliseconds, then performing the modulo operator (%) on it.
The last few digts of the time in milliseconds are rather random (the few first digits aren't).
For example, if you want a random range of integers between 0 and 4, you can do this:

the limitation of the above is:
1: you cant get the system time too often (not more than about once a second, otherwise its not very random from the previous call of system time).
2: you can't have a range more than about 100 or so (which is getting close to 1000 milliseconds).

*****
I doubt many programmers would come up with a solution in an interview unless it was covered in some college class.
Although I would be impressed if you had the answer in the interview, I wouldn't penalize you for not knowing it. I'll be more interested in your thought process on trying to solve it than a correct answer. Lastly, I suggest you don't listen to any comments that discourage you from programming. Have the determination
and drive to never, ever, ever give up no matter how many setbacks.

936517 wrote:
Although I would be impressed if you had the answer in the interview, I wouldn't penalize you for not knowing it. I'll be more interested in your thought process on trying to solve it than a correct answer.

936517 wrote:
One (poor) way of solving this is to get the current system time in milliseconds, then performing the modulo operator (%) on it.
The last few digts of the time in milliseconds are rather random (the few first digits aren't).
For example, if you want a random range of integers between 0 and 4, you can do this:

Same soluton I came up with on the fly. The interviewers are looking to see if you can think on your feet and that is usually the short answer and probably the solution they are looking for, rather than a full blown random number generator.

Please drop the following sentence from my previous post. It's not relevant the issue at hand.
"The last few digits of the time in milliseconds are rather random (the few first digits aren't)"

By the way, I reached my solution by the following methods:
1: I searched Google for key words such as 'java random number generator', but didn't find anything relevant (then again, I didn't look too long)
2: I recalled that the java random function uses current time as a seed for generating numbers. I then realized that the last few digits of the time in milliseconds is random if you don't get the time too soon after the last one.
3: Java modulo has something to do with random numbers.
4: I wrote a simple program to get the current time in milliseconds and apply modulo to it.
I then examined the result to see if it 'looked' random.
5: I reflected on what possible limitations there was to my solution.

Sometimes its interesting to see what people come up with though. Generally I have an aversion for these threads because I fail so horribly at producing an answer myself, I'm really not the "on the spot" type of guy. I focus on understanding and being able to grasp the big picture, not keeping the trivial memorized. Don't ask me about sorting algorithms, complex SQL problems and design patterns because I don't care about keeping that stuff prepared; I have it stacked away deep in the back of my head and I use a clever Google query to yank it out into my consciousness when I need it!