Thread: Random Integers
View Single Post
  #5 (permalink)  
Old 12-08-2007, 08:29 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Its an idea of how to explore a question.
Code:
private static void test(int low, int high) { // Find the lowest and highest random values. // Save the extreme values in variables to print later. // Initialize the minimum variable to the highest // possible value for its type (data type int) int min = Integer.MAX_VALUE; // Initialize the maximum value to the lowest // possible value for its data type. int max = -Integer.MAX_VALUE; // Compute random values in the range [low <= value <= high]. // This is the part you play with to get the results you want. int range = high - low +1; // Do enough runs to get definite accurate min and max: for(int j = 0; j < 500; j++) { int n = low + seed.nextInt(range); // If the value "n" is lower than the // current/last-saved value of "min" // save the value of "n" in "min". if(n < min) min = n; // Update "max" with the highest value we find. if(n > max) max = n; } // Print out the results. System.out.printf("min = %d max = %d%n", min, max); }
All of this was a journey to be able to write this:
Code:
int n = low + seed.nextInt(high - low +1);
with confidence.
Reply With Quote