Java Reference
In-Depth Information
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(intx=0;x<10000; x++)
{
num = (int)(Math.floor(Math.random() * 10));
dist[num]++;
}
// Display distribution of random integers in the range
//0to9
System.out.println(“Distribution using Math.random() ”);
for(intk=0;k<10;k++)
System.out.print(k + “\t”);
// Display results
for(inty=0;y<10;y++)
{
System.out.print(dist[y] + “\t”);
}
System.out.println();
}
}
On the Web
The program RandNum.java is located in the Chapter 23 folder at
www.crcpress.com .
Search WWH ::




Custom Search