techpreparation-homepage

Home  Interview Questions  Aptitude Questions  Tutorials  Placement Papers  Search  Resume Guide  Soft Skills  Video  Forum  Blog


Technical Interview Questions
Javascript Interview Questions
Oracle Interview Questions
J2EE Interview Questions
C++ Interview Questions
XML Interview Questions
EJB Interview Questions
JSP Interview Questions
                              .........More

Programming Source Codes
Java Source Codes
Html Source Codes
CSS Source Codes
C Source Codes
                              .........More

Soft Skills
Communication Skills
Leadership Skills
                              .........More

Subscribe to our Newsletters
Name:
Email:

 

 

  

Java Source Codes

random number generator

package corejava;

/**
An improved random number generator
Gives a set of random integers that does not exhibit
as much correlation as the method used by the Java random number generator.

*/

public class RandomIntGenerator
{ /**
Constructs an object that generates random integers in a given range
@param l the lowest integer in the range
@param h the highest integer in the range
*/

public RandomIntGenerator(int l, int h)
{ low = l;
high = h;
}

/**
Generates a random integer in a range of integers
@return a random integer
*/

public int draw()
{ int r = low
+ (int)((high - low + 1) * nextRandom());
return r;
}

/**
test stub for the class
*/

public static void main(String[] args)
{ RandomIntGenerator r1
= new RandomIntGenerator(1, 10);
RandomIntGenerator r2
= new RandomIntGenerator(0, 1);
int i;
for (i = 1; i <= 100; i++)
System.out.println(r1.draw() + " " + r2.draw());
}

private static double nextRandom()
{ int pos =
(int)(java.lang.Math.random() * BUFFER_SIZE);
if (pos == BUFFER_SIZE) pos = BUFFER_SIZE - 1;
double r = buffer[pos];
buffer[pos] = java.lang.Math.random();
return r;
}

private static final int BUFFER_SIZE = 101;
private static double[] buffer
= new double[BUFFER_SIZE];
static
{ int i;
for (i = 0; i < BUFFER_SIZE; i++)
buffer[i] = java.lang.Math.random();
}

private int low;
private int high;
}


<<<----- Return to Java Source Code Questions Page.


 

Have a Question ? post your questions here. It will be answered as soon as possible.

Check Java Interview Questions for more Java Interview Questions with answers

Check Servlet Interview Questions for more Servlet Interview Questions with answers

Check Structs Interview Questions for more Structs Interview Questions with answers