I need help with homework


Joined
Jan 8, 2022
Messages
11
Reaction score
0
i need help. i am trying to generate 2000 numbers between the values of 10 and 30, then use a one dimensional array to record the frequency of each numbers.
problem is, i am very confused and dont know how to do this. how do i do this? here is the code.

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
double[] arr = new double[2000];
Random randNum = new Random();
for (int i = 0; i < 2000; i++) {
arr = randNum.nextInt();
}
}
}
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
174
Reaction score
19
I appreciate you posting what you have so far (next time, try code tags). So, to start, the array that you're creating should be an integer array that covers the range of values you're generating, so we'll use a size of 30. Next, to generate a random number between two numbers, you use randNum.nextInt(RANGE) + MINIMUM (RANGE being MAXIMUM - MINIMUM). Last, to store the frequency, use the random number you've generated as the array index and increment that value.

Java:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
class Goldenrtvr {
        public static void main(String[] args) {
                int[] arr = new int[30];
                Random randNum = new Random();
                for (int i = 0; i < 2000; i++) {
                        arr[randNum.nextInt(20) + 10]++;
                }
                for (int i=10;i<30;i++){
                        System.out.println(i + ": " + arr[i]);
                }
        }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top