1

I'm trying to write a program that will store the count of a random number into a single dimensional array. I'm getting stuck when assigning the number count to the array. This is a logic error and it compiles just fine. Thanks.

public class PP67 { public static void main(String[] args) { int zero =0, one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0, nine=0; int[] counts = new int[10];//create array int i; for(i = 0; i < 100; i++) { int rand = (int)(Math.random()*10); //generate 100 random ints between 0-9 if (rand == 1){ //assign the random number counter to the array one++; counts[0] = one; } else if (rand == 2) { two++; counts[1] = two; } else if (rand == 3) { three++; counts[2] = three; } else if (rand == 4) { four++; counts[3] = four; } else if (rand == 5) { five++; counts[4] = five; } else if (rand == 6) { six++; counts[5] = six; } else if (rand == 7) { seven++; counts[6] = seven; } else if (rand == 8) { eight++; counts[7] = eight; } else if (rand == 9) { nine++; counts[8] = nine; } else if (rand == 0) { zero++; counts[9] = zero; } } System.out.println("The count for number 1 is: " + counts[0]); //need outside count loop System.out.println("The count for number 2 is: " + counts[1]); System.out.println("The count for number 3 is: " + counts[2]); System.out.println("The count for number 4 is: " + counts[3]); System.out.println("The count for number 5 is: " + counts[4]); System.out.println("The count for number 6 is: " + counts[5]); System.out.println("The count for number 7 is: " + counts[6]); System.out.println("The count for number 8 is: " + counts[7]); System.out.println("The count for number 9 is: " + counts[8]); System.out.println("The count for number 0 is: " + counts[9]); } } 
4
  • What, specifically, is the problem you are having? Commented Mar 28, 2014 at 21:28
  • Try incrementing BEFORE you set it to the array. Commented Mar 28, 2014 at 21:28
  • The output shows 0 for each of the 10 array slots. It's not actually counting the random numbers for me. I need the program to count the number of times it comes up with the random number. Commented Mar 28, 2014 at 21:31
  • After incrementing beforehand and changing the rands precedence, I was able to get this running. Thanks all. @RUJordan n' Zou Commented Mar 28, 2014 at 21:45

2 Answers 2

3

The problem is here:

int rand = (int)Math.random()*10; 

The cast as a higher precedence than the multiplication.

Since Math.random returns a double in [0, 1[ (as the documentation states), casting it to int after will always end up with a 0. So you're always doing 0 * 10. Hence rand is always 0.

Multiply the random number by 10 first and then cast it to int.

int rand = (int)(Math.random()*10); 

Also you could use the nextInt(int n) method from the Random class.

Random r = new Random(); int rand = r.nextInt(10); //will generate a random number in [0, 9] 
Sign up to request clarification or add additional context in comments.

1 Comment

For those who don't know, [0, 1[ means "greater than or equal to zero and less than one." Some might be more used to [0, 1)
0

1) You don't need to keep the same information both in variables one, two three etc and in the array counts.

2) The expression int rand = (int)(Math.random()*10) always returns 0. To generate random number in [0,9], it is better to use

Random r = new Random(); int rand = r.nextInt(10); 

3) You don't need all these if-else if conditions, you can use the random number as index of the array counts.

private final static int REP = 100; private final static int MAX = 10; public static void main(String[] args) { Random rand = new Random(System.currentTimeMillis()); int[] counts = new int[MAX]; for(int i=0; i<REP; i++) { int n = rand.nextInt(MAX); counts[n]++; } for(int i=0; i<MAX; i++) { System.out.printf("The count for number %d is: %d \n", i, counts[i] ); } } 

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.