3

I'm lost as how to compare 100 randomly generated number between 0-9 to an array value, also between 0-9, and then print the results. Be easy on me, I'm new to coding and I know I suck. I feel as though I'm 75% there. I know there are ways to make some of the code less redundant, however I seem to struggle with those techniques.

Here's what I have so far:

public static void main(String[] args) { double randomNum = 0; for (int i = 0; i < 100; i++) { randomNum = Math.random() * 10; int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0; int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0; int [] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for (double j = 0; j <arrayNums.length; j++){ if (arrayNums[0] == randomNum) { count0++; } else if (arrayNums[1] == randomNum){ count1++; } else if (arrayNums[2] == randomNum){ count2++; }else if (arrayNums[3] == randomNum){ count3++; }else if (arrayNums[4] == randomNum){ count4++; }else if (arrayNums[5] == randomNum){ count5++; }else if (arrayNums[6] == randomNum){ count6++; }else if (arrayNums[7] == randomNum){ count7++; }else if (arrayNums[8] == randomNum){ count8++; } else{ count9++; } } System.out.print("Occurrences of 0: " + count0); System.out.print("\nOccurrences of 1: " + count1); System.out.print("\nOccurrences of 2: " + count2); System.out.print("\nOccurrences of 3: " + count3); System.out.print("\nOccurrences of 4: " + count4); System.out.print("\nOccurrences of 5: " + count5); System.out.print("\nOccurrences of 6: " + count6); System.out.print("\nOccurrences of 7: " + count7); System.out.print("\nOccurrences of 8: " + count8); System.out.print("\nOccurrences of 9: " + count9); } } 

}

Any and all help is appreciated.

4
  • 1
    Oh look. It's a tree. I wonder where the forest is? You've already got one array and one loop, but then you go arrayNums[1], arrayNums[2] etc. You could use another array (of counts) and ore looping to shrink this to about 10 lines of code. Commented Feb 12, 2015 at 5:48
  • You could use a HashMap for this . It will be kind of easy. Number --> Count pair will work for you. Commented Feb 12, 2015 at 5:49
  • @TheLostMind the question says "With An Array" Commented Feb 12, 2015 at 5:51
  • 2
    @RandykaYudhistira - That's why I commented instead of answering it. There is no harm in specifying that there are better ways of doing certain things.:) Commented Feb 12, 2015 at 5:54

5 Answers 5

2
  • Using a HashMap as a counter is more elegant than keeping a separate variable for each item.
  • Use Random.nextInt(10) to generate a random number between 0 to 9 (inclusive).

     Map<Integer, Integer> counter = new HashMap<Integer, Integer>(); Random rand = new Random(); for (int i = 0; i < 100; i++) { int randomNum = rand.nextInt(10); Integer currentCount = counter.get(randomNum); if (null == currentCount) { counter.put(randomNum, 1); } else { counter.put(randomNum, currentCount+1); } } for (Integer key : counter.keySet()) { System.out.println(key + " -> " + counter.get(key)); } 

Example output

0 -> 12 1 -> 10 2 -> 9 3 -> 6 4 -> 8 5 -> 9 6 -> 11 7 -> 12 8 -> 14 9 -> 9 
Sign up to request clarification or add additional context in comments.

4 Comments

the question says with an array
@RandykaYudhistira The OP could also ask "how can I get across the canal swimming?" and it would be stupid to show him how instead of teaching him that he can use a boat... Further, even if he was restricted to use the array, it's not very difficult to copy it into the hashmap as an initialization stage: for (int i : arr) counter.put(i, 0); - right ?
I'd at least recommend a TreeMap, rather than a HashMap so that you get nice ordered output.
@MichaelAnderson sure you can, but as you will see (even if you'll run it 1000 times) even with a hashMap the output is ordered ;)
1

Well, instead of using

int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0; int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0; 

You can use:

int[] count = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 

And your method will look like:

 public static void main(String[] args) { int[] count = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int[] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for (int i = 0; i < 100; i++) { int randomNum = (int) (Math.random() * 10); for (int j = 0; j < 10; j++) { if (arrayNums[j] == randomNum) { count[j]++; } } } for (int i = 0; i < 10; i++) { System.out.println("Occurrences of " + i + ": " + count[i]); } } 

Comments

1

First add all values to array. Then iterate the array and store the value as key in a HashMap and values as a count of occurrences. Then after iteration you will have what you trying to archive here.

Eg:

 int[] arr = {0, 1, 0, 2, 5, 2, 4, 6, 0, 4, 7, 8, 9, 0, 2, 5, 7, 6}; Map<Integer, Integer> countMap = new HashMap<>(); for (int i : arr) { Integer currentCount = countMap.get(i); if (currentCount != null) { countMap.put(i, currentCount + 1); } else { countMap.put(i, 1); } } for(Map.Entry<Integer,Integer> entry:countMap.entrySet()){ System.out.println("Number of occurrences of "+entry.getKey() +" : "+entry.getValue()); } 

1 Comment

@Downvoter you should have a courage to put a comment when down voting.... It will help to get clear understanding for you, me and all others.
0

You are resetting your counters back to 0 every iteration of the for loop. And I don't know what the second loop is for.

 public static void main(String[] args) { double randomNum = 0; int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0; int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0; for (int i = 0; i < 100; i++) { randomNum = Math.random() * 10; switch(randomNum) { case 0: count0++; break; case 1: count1++; break; case 2: count2++; break; case 3: count3++; break; case 4: count4++; break; case 5: count5++; break; case 6: count6++; break; case 7: count7++; break; case 8: count8++; break; case 9: count9++; break; } } System.out.print("Occurrences of 0: " + count0); System.out.print("\nOccurrences of 1: " + count1); System.out.print("\nOccurrences of 2: " + count2); System.out.print("\nOccurrences of 3: " + count3); System.out.print("\nOccurrences of 4: " + count4); System.out.print("\nOccurrences of 5: " + count5); System.out.print("\nOccurrences of 6: " + count6); System.out.print("\nOccurrences of 7: " + count7); System.out.print("\nOccurrences of 8: " + count8); System.out.print("\nOccurrences of 9: " + count9); } 

Comments

0

You call the random in wrong way. it should be like this :

Random rnd = new Random(); int randomNum = rnd.nextInt(10); 

And you put count variable in wrong way too. Then your full code will be like this :

public static void main(String[] args) { Random random = new Random(); int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0; int count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0; for (int i = 0; i < 100; i++) { int randomNum = random.nextInt(10); int [] arrayNums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; for (double j = 0; j <arrayNums.length; j++){ if (arrayNums[0] == randomNum) { count0++; } else if (arrayNums[1] == randomNum){ count1++; } else if (arrayNums[2] == randomNum){ count2++; }else if (arrayNums[3] == randomNum){ count3++; }else if (arrayNums[4] == randomNum){ count4++; }else if (arrayNums[5] == randomNum){ count5++; }else if (arrayNums[6] == randomNum){ count6++; }else if (arrayNums[7] == randomNum){ count7++; }else if (arrayNums[8] == randomNum){ count8++; } else{ count9++; } } } System.out.print("Occurrences of 0: " + count0); System.out.print("\nOccurrences of 1: " + count1); System.out.print("\nOccurrences of 2: " + count2); System.out.print("\nOccurrences of 3: " + count3); System.out.print("\nOccurrences of 4: " + count4); System.out.print("\nOccurrences of 5: " + count5); System.out.print("\nOccurrences of 6: " + count6); System.out.print("\nOccurrences of 7: " + count7); System.out.print("\nOccurrences of 8: " + count8); System.out.print("\nOccurrences of 9: " + count9); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.