Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to solve it in Java. We can use the Math.random method to generate random

ID: 3769961 • Letter: I

Question

I need to solve it in Java.

We can use the Math.random method to generate random integers. For example, Math.random() generates a random integer greater than or equal to 0 and less than 1. The expression Math.random() * 6 generates random numbers between 0 and 5, simulating the throw of a die. In this lab assignment, you will use an array to test whether the random generator is fair; that is, whether each possible value is generated approximately the same number of times.

Your program should ask the user:

            How many random numbers should be generated?

            What is the number of values for each random draw? (e.g., 6)

Make an array with one element for each possible outcome. Set each element to 0. Then keep calling the random number generator. If it returns a value v, then increment the counter belonging to v.

After all numbers have been generated, print the counters. Here is a typical program run:

What is the code for your program?

Explanation / Answer

program:

import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class RandomNumber {
    public static void main(String []args)
    {
    int totalnum,rang,num,frequency[];
    Scanner scan=new Scanner(System.in);
    System.out.println("How many random numbers should be generated?");
    totalnum=scan.nextInt();
    System.out.println("What is the number of values for each random draw?");
    rang=scan.nextInt();
    frequency=new int[rang];
    Random ran=new Random();
    for(int i=0;i<totalnum;i++)
    {
    num=ran.nextInt(rang)%rang;
    frequency[num]++;
    }
    System.out.println("digit   frequency");
    for(int i=0;i<rang;i++)
    {
    System.out.println(i+"      "+frequency[i]);
    }
    }
    }
  

import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class RandomNumber {
    public static void main(String []args)
    {
    int totalnum,rang,num,frequency[];
    Scanner scan=new Scanner(System.in);
    System.out.println("How many random numbers should be generated?");
    totalnum=scan.nextInt();
    System.out.println("What is the number of values for each random draw?");
    rang=scan.nextInt();
    frequency=new int[rang];
    Random ran=new Random();
    for(int i=0;i<totalnum;i++)
    {
    num=ran.nextInt(rang)%rang;
    frequency[num]++;
    }
    System.out.println("digit   frequency");
    for(int i=0;i<rang;i++)
    {
    System.out.println(i+"      "+frequency[i]);
    }
    }
    }
  

output:

How many random numbers should be generated?
1000
What is the number of values for each random draw?
10
digit   frequency
0      91
1      92
2      101
3      100
4      91
5      91
6      108
7      86
8      121
9      119