2 Years
Discussion Span
Last Post by rubberman
Featured Replies
  • <Sigh> I'm sure you know this by now but... Do provide evidence of having done some work yourself if posting questions from school or work assignments (DaniWeb Member Rules) Read More


I'm sure you know this by now but...

Do provide evidence of having done some work yourself if posting questions from school or work assignments
(DaniWeb Member Rules)


@stultuske,I have done same thing what you have did it now. But how to do it without using Random().


vivek: you want to do it without "library function". since Java doesn't have functions (it has methods) ... what's the problem ?
in the end, you can not possibly do anything in Java without using any existing library, so be more specific about what you can and can not use. that, and don't disregard James' remark.

This fora are about helping you to increase your knowledge and improve your work, not about doing the work for you.

import java.util.Random;

public class Randnum {
    public static void integerRand(int end,int start,Random random){

                long range = (long)end - (long)start + 1;

    long fraction = (long)(range * random.nextDouble());
    int randomNumber =  (int)(fraction + start);    
        System.out.println("Generated "+randomNumber);
    public static void main(String[] args) {
    int START = 1;
    int END = 10;
    Random random = new Random();
    for (int idx = 1; idx <= 10; ++idx){
      integerRand(START, END, random);


OK, that's an example of code that DOES use the API Random class (although in a rather bizarre way).
I thought you wanted a solution that does NOT use a standard "library function"


This question was asked me in interview and i have given the above solution.I wanted to confirm from you guys whether it is ok or not.
Result still to come.

@stultuske, i know there are methods in java but answer to that question has to be given in any preffered language that's why written "library function".

Now can you friends tell me this solution is worth or not?


Sorry, but that does not meet the requirement of "without using library function", so it's no good.
You need to Google for random number generator algorithms, and implement one of those in "raw" Java.


I don't know why you would have an interview question like that... It is NOT trivia to implement a pseudo RNG without using any API. Here is a link to some algorithms. I learned it in school a long time ago, and I never used it anymore. One common thing you need is a seed value. Normally, it is one or a set of prime numbers going in the calculation (or on the modern day, use current time at milli or nano second).

Edited by Taywin


Go to this site, and study it closely. It has the best RNG tutorials available: http://www.phy.ornl.gov/csep/CSEP/RN/RN.html

From the site (I've posted this here before, at least twice):

Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin. -- John von Neumann (1951)

Anyone who has not seen the above quotation in at least 100 places is probably not very old. -- D. V. Pryor (1993)

All that aside, getting random number generators right is VERY difficult. Myself, I like the lagged fibonacci routines. Fast, and pretty decent in output. In any case, anyone who writes a pseudo-random number generator is, in the words of Saint von Neumann, in a state of sin. Just how much you are sinning varies! :-)

Edited by rubberman

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.