Solve Confused Java HW: Generate 225 Samples Size 625 Random Nos U(10,22)

In summary: I also noticed that in your main you have the variable lowercase I for the for loop and an uppercase I for indexing your array. Remember in Java, variables are case-sensitive.
  • #1
Frosty415
3
0
  1. Generate 225 samples of size 625 random numbers from U(10,22). For each of these 225 samples calculate the mean.
a) Find the simulated probability that the mean is between 10 and 11.

b) Find the mean of the means.

c) Find the standard deviation of the means.

d) Draw the histogram of the means.

Here's my code: I am unable to print from my method of probabilityMean. Anyone know what's wrong?
package test;

Java:
import java.util.Random;

public class test {

    public static double average(int[] x) {

        double sum = 0;

        for (int j = 0; j < x.length; j++) {
            sum += x[j];
        }

        double average = sum / x.length;

        return average;

    }

    public static double average(double[] x) {

        double sum = 0;

        for (int j = 0; j < x.length; j++) {
            sum += x[j];
        }

        double average = (sum) / x.length;

        return average;

    }

    public static double probabilityMean(double[] x) {
        int count = 0;
        double avg = average(x);
        if (avg >= 10 && avg <= 11) {
            count++;
        }
        double probab = (float) count / 625;
        return probab;
    }

    public static double standardDeviation(double[] values) {

        double deviation = 0.0;

        if ((values != null) && (values.length > 1)) {

            double mean = average(values);

            for (double value : values) {

                double delta = value - mean;

                deviation += delta * delta;

            }

            deviation = Math.sqrt(deviation / values.length);

        }

        return deviation;

    }

    public static void main(String[] args) {
        Random rand = new Random();

        double finalmean, sd;

        double[] mean = new double[225];

        int[][] x = new int[225][625];
        for (int j = 0; j < 225; j++) {
            for (int k = 0; k < 625; k++) {
                x[j][k] = rand.nextInt((22 - 10) + 1) + 10;
            }
            mean[j] = average(x[j]);
            System.out.println("Mean of array" + j + " is " + mean[j]);

        }

        finalmean = average(mean);

        sd = standardDeviation(mean);

        System.out.println("Mean of means:" + finalmean + "\nStandard deviation:" + sd);
    }

}
Mod edit: To deal with the problem of i index turning to I and being treated as italics tags, I changed the index i to j, and in one method with both i and j, changed i to j and j to k.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
You defined it but where are you calling it from?

Also I edited your post adding code tags for pretty printing it.

I also noticed that in your main you have the variable lowercase I for the for loop and an uppercase I for indexing your array. Remember in Java, the variable case name is important, so Java thinks They are two different variables.
 
  • #3
jedishrfu said:
You defined it but where are you calling it from?

Also I edited your post adding code tags for pretty printing it.

I also noticed that in your main you have the variable lowercase I for the for loop and an uppercase I for indexing your array. Remember in Java, the variable case name is important, so Java thinks They are two different variables.
That's really weird because on netbeans it is all lower case for me.
 
  • #4
Also, I tried calling it into the main method but kept receiving errors. I deleted it on the code provided because of the errors. I wanted to see if anyone was able to call upon the method and print from it. not sure if the method is wrong or I'm calling it wrong.
 
  • #5
So what did you do and what are the errors youre seeing?

From your code i see all your methods are static meaning they are class methods not instance methods. Flexibility is improved if you make them instance methods but since the class doesn't define any variable then its okay for now.

I did notice your cast in the probabilityMean where youre doing an integer divide casting it as a float and then assigning it to a double. Doesn't that strike you as odd? Why not make the 625 a double ie 625.0 and drop the cast?
 
  • #6
Frosty415 said:
That's really weird because on netbeans it is all lower case for me.
not sure about netbeans, but when I paste your code into eclipse I see 6 error messages relating to upper case "I" being used. When I change the 6 upper case "I"s to lower case I am able to compile and run your Class and the results seem pretty reasonable.
 
  • #7
gabbagabbahey said:
not sure about netbeans, but when I paste your code into eclipse I see 6 error messages relating to upper case "I" being used. When I change the 6 upper case "I"s to lower case I am able to compile and run your Class and the results seem pretty reasonable.
It could be that the OP typed i, but some spell-check setting somewhere is assuming that it really should be I (upper-case i).
 
  • #8
Yeah, I wasn't sure what happened. I edited the post to add code tags and the end listing showed up with italics tags that's the [ ] with a /I inside which appeared to come from the usage of the I for indexing.
 
  • #9
jedishrfu said:
Yeah, I wasn't sure what happened. I edited the post to add code tags and the end listing showed up with italics tags that's the [ ] with a /I inside which appeared to come from the usage of the I for indexing.
I think you're right. Inside a pair of code tags, if you use i as the index into an array, the BBCode rendering part of things thinks you meant [ I ], for italics. Even if you use lower case i, it still thinks this is an italics tag, and capitalizes the letter i. As far as I know, there's no workaround other than to not use i as an index in arrays.
 
  • #10
Wow software bugs within bugs. This would make a great sci-fi story plot. The listing you see is not the code that is running.

Aside:

I saw code like this on a Commodore 64 to fake out the teacher. The student would call the teacher over and say my programs not working. Listing it would show something real simple but when run a different answer is shown. Unbeknownst to the teacher was that the real program is actually responding to the list and run typed input and displaying faked output.

It's a great trick to play naive computer science teachers.
 

FAQ: Solve Confused Java HW: Generate 225 Samples Size 625 Random Nos U(10,22)

How do I generate a sample size of 225 with 625 random numbers between 10 and 22 in Java?

To generate a sample size of 225 with 625 random numbers between 10 and 22 in Java, you can use the Random class and its nextInt method. First, create a Random object and then use a for loop to generate 625 random numbers. Within the loop, use nextInt(13) + 10 to generate a random number between 10 and 22. Store these numbers in an array or list to create a sample size of 225.

How can I ensure that the generated random numbers are uniformly distributed between 10 and 22?

To ensure that the generated random numbers are uniformly distributed between 10 and 22, you can use the formula nextInt(13) + 10 as mentioned in the previous question. This will generate random numbers with equal probability within the specified range.

Can I use a different range for the generated random numbers?

Yes, you can use a different range for the generated random numbers by changing the arguments in the nextInt method. For example, if you want the random numbers to be between 5 and 15, you can use nextInt(11) + 5.

How can I display the generated sample size and random numbers in Java?

To display the generated sample size and random numbers in Java, you can use System.out.println() or System.out.print() statements within the for loop. This will print the values on the console. You can also store the generated numbers in an array or list and then use a for loop to print them.

Is there a way to generate a sample size of 225 with 625 unique random numbers in Java?

Yes, you can generate a sample size of 225 with 625 unique random numbers in Java by using the Random class and its nextInt method with a Set data structure. First, create a Set object to store the generated numbers. Then, use a while loop to generate random numbers and add them to the Set until it contains 625 unique numbers. Finally, convert the Set to an array or list to get the desired sample size.

Similar threads

Replies
7
Views
2K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
4
Views
1K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Back
Top