RandNum.java Source code

Java tutorial

Introduction

Here is the source code for RandNum.java

Source

/*
Java Programming for Engineers
Julio Sanchez
Maria P. Canton
    
    
ISBN: 0849308100
Publisher: CRC Press
*/

// Java for Engineers
//Filename: RandNum
//Reference: Chapter 23
//Description:
//         Generating random numbers

class RandNum {
    public static void main(String[] args) {
        int num;
        int[] dist = new int[10]; // Storage for distribution

        // Generate 10000 random numbers using Math.random()
        for (int x = 0; x < 10000; x++) {
            num = (int) (Math.floor(Math.random() * 10));
            dist[num]++;
        }
        // Display distribution of random integers in the range
        // 0 to 9
        System.out.println("Distribution using Math.random() ");
        for (int k = 0; k < 10; k++)
            System.out.print(k + "\t");
        // Display results
        for (int y = 0; y < 10; y++) {
            System.out.print(dist[y] + "\t");
        }
        System.out.println();
    }
}