This discussion is archived
1 2 3 Previous Next 32 Replies Latest reply: Mar 28, 2013 4:42 PM by EJP RSS

Do not use java built in Random Function to generate random number.

706710 Newbie
Currently Being Moderated
Language Core Java 1.5 (do not use Random function of java)

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   
}  
  • 1. Re: Do not use java built in Random Function to generate random number.
    PhHein Guru Moderator
    Currently Being Moderated
    There are no functions in Java! What have you done so far? What code did you try? Where are you stuck? Nobody will do your homework for you.

    And use a better topic title next time.
  • 2. Re: Do not use java built in Random Function to generate random number.
    706710 Newbie
    Currently Being Moderated
    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

    Edited by: Deepak Lalchandani on Mar 21, 2013 4:48 PM
  • 3. Re: Do not use java built in Random Function to generate random number.
    Kayaman Guru
    Currently Being Moderated
    Wait, the same method should return either 0-4 or 0-7? That doesn't make any sense.

    But I guess it doesn't matter, you clearly don't know enough Java to be hired.
  • 4. Re: Do not use java built in Random Function to generate random number.
    706710 Newbie
    Currently Being Moderated
    I'm not here to post questions of my choice.
    this was a question asked in a core java technical interview.
  • 5. Re: Do not use java built in Random Function to generate random number.
    gimbal2 Guru
    Currently Being Moderated
    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.
  • 6. Re: Do not use java built in Random Function to generate random number.
    939520 Explorer
    Currently Being Moderated
    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:
     
            Date date = new Date(); 
            long milliseconds = date.getTime(); 
            int range = 5; 
            System.out.println(milliseconds % range); 
    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.
  • 7. Re: Do not use java built in Random Function to generate random number.
    gimbal2 Guru
    Currently Being Moderated
    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.
    That is a sign of a good interviewer to me :)
  • 8. Re: Do not use java built in Random Function to generate random number.
    morgalr Explorer
    Currently Being Moderated
    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:
     
    Date date = new Date(); 
    long milliseconds = date.getTime(); 
    int range = 5; 
    System.out.println(milliseconds % range); 
    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.
  • 9. Re: Do not use java built in Random Function to generate random number.
    Kayaman Guru
    Currently Being Moderated
    gimbal2 wrote:
    It has little to do with Java, this is a generic trivia question that is not language-specific.
    Indeed, Java is not the issue with the question. However, this is a Java forum and I'd imagine that the position he was applying was Java related.

    I have a big aversion towards these "I was asked X in the interview, what is the correct answer?" questions, and I don't think I need to explain why...
  • 10. Re: Do not use java built in Random Function to generate random number.
    939520 Explorer
    Currently Being Moderated
    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.

    Edited by: 936517 on Mar 21, 2013 12:46 PM
  • 11. Re: Do not use java built in Random Function to generate random number.
    gimbal2 Guru
    Currently Being Moderated
    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!
  • 12. Re: Do not use java built in Random Function to generate random number.
    EJP Guru
    Currently Being Moderated
    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)
    Hard to believe. When I did that, the first four results were about built-in classes, but from there on I started getting user-written code.
    3: Java modulo has something to do with random numbers.
    No it doesn't. The converse is true, that random number generation generally uses modulus operations. Java has nothing to do with it either way.
  • 13. Re: Do not use java built in Random Function to generate random number.
    EJP Guru
    Currently Being Moderated
    My answer would have been "it's in Knuth Volume I" (or II or III as appropriate).
  • 14. Re: Do not use java built in Random Function to generate random number.
    Kayaman Guru
    Currently Being Moderated
    EJP wrote:
    My answer would have been "it's in Knuth Volume I" (or II or III as appropriate).
    Coupled with an icy stare at the interviewer, no doubt.
1 2 3 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points