This code displays the number of random Gaussian numbers per console (10 per line) and shows some statistics (lowest, highest and average).
If you try it with a small account number, random numbers will probably be in the range [-1.0 ... +1.0], and the average value may be in the range [-0.1 ... +0.1]. However, if the number exceeds 10.000, random numbers will probably fall in the range [-4.0 ... +4.0] (more incredible numbers may appear at both ends), although the average value can be in the range [-0.001 ... +0.001 ] (closer to 0).
public static void main(String[] args) { int count = 20_000; // Generated random numbers double lowest = 0; // For statistics double highest = 0; double average = 0; Random random = new Random(); for (int i = 0; i < count; ++i) { double gaussian = random.nextGaussian(); average += gaussian; lowest = Math.min(gaussian, lowest); highest = Math.max(gaussian, highest); if (i%10 == 0) { // New line System.out.println(); } System.out.printf("%10.4f", gaussian); } // Display statistics System.out.println("\n\nNumber of generated random values following Gaussian distribution: " + count); System.out.printf("\nLowest value: %10.4f\nHighest value: %10.4f\nAverage: %10.4f", lowest, highest, (average/count)); }
Hardzsi
source share