# Counting and Math.random

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• March 7th, 2014, 07:52 PM
Elyril
Counting and Math.random
Hey, so I need some major help with this problem.. maybe not major, but I'm trying to generate random 100 numbers, from 0 to 9, in an array using Math.random, but it only outputs 0s which is very confusing to me. Also, I need to be able to count how many different integers there are like 0s, 1s, 2s... 8s, 9s. Also confused on how to do that exactly, thought I had it, but now I just confused my whole brain. Please help. Here's my code, I only got as far as the array then got stumped on the counting part. Thank you in advance!
Code Java:

```import java.util.Arrays;   public class countDigits { public static void main(String[] args) {   //Create random generator and values int numbers = (int)(Math.random() * 10); int arrayCount = 1; final int numOfArrays = 100; int[] digitArray = new int[numOfArrays]; int counts = 0;   //Create array for (arrayCount = 1; arrayCount <= digitArray.length; arrayCount++) { System.out.print(digitArray[numbers] + " | "); } //Count different integers   } }```
and my results
Code Java:

`0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0`
• March 7th, 2014, 08:05 PM
Norm
Re: Counting and Math.random
Where does the code assign any values to the elements in the digitArray array?
There needs to be a loop that indexes through the elements of the array and assigns a number to each element.
• March 7th, 2014, 08:28 PM
Elyril
Re: Counting and Math.random
how would I do that exactly with 100 different arrays? The programs I've seen only do something like
Code Java:

```array[0] = 0 array[1] = 2 array[2] = 5   //etc.```
• March 7th, 2014, 08:30 PM
Norm
Re: Counting and Math.random
Instead of the numbers: 0,1,2 as the index, use the for loop control variable as the index.
• March 7th, 2014, 08:50 PM
Elyril
Re: Counting and Math.random
Okay, so I put this in the array loop, not sure if it matters, but now the numbers change, so instead of 0 it would be 3 or 7, but they're all that same number.
Code Java:

```for (arrayCount = 1; arrayCount <= digitArray.length; arrayCount++) { System.out.print(arrayCount + ": "); System.out.print((digitArray[arrayCount] = numbers) + " | "); }```
But then it also says at the end.
Code Java:

```94: 8 | 95: 8 | 96: 8 | 97: 8 | 98: 8 | 99: 8 | 100: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100 at countDigits.main(countDigits.java:20)```
The numbers are just there to tell me how many there are.
• March 7th, 2014, 09:07 PM
Norm
Re: Counting and Math.random
If you want random numbers assigned to the array, you need to use the Random class to create each number that is assigned to an element of the array. Like is assigned to the variable: numbers

Quote:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
The index to the array went past the end. Remember that the indexes for an array range in value from 0 to the length of the array minus 1. For a 10 element array, the max index would be 9.
Check that the for loop index does not get to be equal to the length of the array.
• March 7th, 2014, 09:25 PM
Elyril
Re: Counting and Math.random
I fix the index problem, but for the random numbers, I have to use the Math.random(); so I'm confused on getting that to work properly. because I have the
Code Java:

```int numbers = (int)(Math.random() * 10); && System.out.print((digitArray[arrayCount] = numbers) + " | ");```
so I don't see exactly what I could be doing wrong because technically I'm assigning values to every element by using the second code right?
• March 7th, 2014, 09:29 PM
Norm
Re: Counting and Math.random
If the value of numbers doesn't change then the same number is assigned to all the elements.
Say numbers is assigned a 7, then all the array elements will be assigned a value of 7.

The Math.random() method returns a different number every time it is called. Use that value like it was used with numbers.
• March 8th, 2014, 05:44 AM
Elyril
Re: Counting and Math.random
Oh, I figured out what you were saying now. Thanks! But now, I'm confused about the counting each integer from that one array.. Some people say to make an array of 10 (0-9) integers then count them that way or to do something way different that confuses me as well.. :/
• March 8th, 2014, 06:23 AM
Norm
Re: Counting and Math.random
Use the random number as the index into the array. Increment the indexed element by 1 to count the random number.
• March 8th, 2014, 09:37 AM
Elyril
Re: Counting and Math.random
Sorry, I don't think I'm understanding well enough. Although I tried to increment each element, it's just the same number for all of them which is what happened before. This was my code
Code Java:

```digitArray[0] = counts++; digitArray[1] = counts++; digitArray[2] = counts++; digitArray[3] = counts++; digitArray[4] = counts++; digitArray[5] = counts++; digitArray[6] = counts++; digitArray[7] = counts++; digitArray[8] = counts++; digitArray[9] = counts++;```
• March 8th, 2014, 09:52 AM
Norm
Re: Counting and Math.random
Treat the element of an int array as an int variable:
Code :

```int[] intArray = new int[10]; // define an array int intVar = 0; // define a variable     intArray[idx]++; // increments the value of intArray[idx]     intVar++; // increments the value of intVar```

Use the random number as the index into the array
• March 9th, 2014, 12:29 PM
Elyril
Re: Counting and Math.random
Okay, so I have no idea what you mean by that to be honest. This is what I have written down.
Code Java:

```for (arrayCount = 1; arrayCount < digitArray.length; arrayCount++) { System.out.print(arrayCount + ": "); System.out.print((digitArray[arrayCount] = (int)(Math.random() * 10)) + " | "); } //Count different integers - new array int[] countVarArray = new int[11]; int countVar = 0;   for (countVarArray[10]++; arrayCount < digitArray.length; countVar++) { System.out.println(); }     System.out.println("\n" + countVar); }```
with this as a result
Code Java:

```1: 5 | 2: 2 | 3: 6 | 4: 2 | 5: 0 | .... | 95: 9 | 96: 9 | 97: 7 | 98: 2 | 99: 9 | 100: 3 | 0```
with the 0 being the count of each integer.. So, I put what you said pretty much, or tried to.
• March 9th, 2014, 12:44 PM
Norm
Re: Counting and Math.random
Get rid of that code and start over.
Make a loop that goes around 100 times: "generate random 100 numbers"
inside that loop generate a random number: "from 0 to 9"
Use that random number to index into an array and select an element
Increment that element by 1 to count the number of random numbers generated with that value.
• March 9th, 2014, 02:29 PM
Elyril
Re: Counting and Math.random
So, do you mean to put another loop inside of the first loop? or put it all in one loop? Sorry I don't really get this.. This is what I put.
Code Java:

```int[] digitArray = new int[101]; int[] counts = new int[10];   //Generate 100 random numbers for (int arrayCount = 0; arrayCount < digitArray.length; arrayCount++) { int randNum = (int)(Math.random() * 10); System.out.print((digitArray[arrayCount] = randNum) + " "); //count for (randNum = 0; randNum < counts.length; randNum++) { System.out.print(randNum); } }```
• March 9th, 2014, 02:38 PM
Norm
Re: Counting and Math.random
I meant there to be a single loop for counting the random numbers.

There could be another loop to print out the results AFTER the counting was done.

The code defines an array to hold the counts but doesn't use it.
Does the assignment say that the values of all the random numbers need to be saved?
Or does it ask for a count of the number of times specific numbers occurred?

You didn't follow my instructions. Where is the code to do this:
Use that random number to index into an array and select an element
Increment that element by 1
That code would be like this: theCountingArray[theRandomNumber]++;
• March 9th, 2014, 03:04 PM
Elyril
Re: Counting and Math.random
The assignment asks that for the program to count the number of times each distinct numbers occurs. So, it wants to show how many 0s, how many 1s, etc.
• March 9th, 2014, 03:10 PM
Norm
Re: Counting and Math.random
Ok, that is what I thought. There are supposed to be 100 randomly chosen values in the range 0 to 9.
Did you rewrite the code?
• March 9th, 2014, 03:36 PM
Elyril
Re: Counting and Math.random
Yep, not sure if this is totally what you meant, but this is what I got from it. I tried to print it by putting System.out.println(counts); but that only gave me '[I@1b845568' and I have no idea what that code is. This is what I have so far.
Code Java:

``` int[] digitArray = new int[101]; int[] counts = new int[10]; int arrayCount = 0;   //Generate 100 random numbers for (arrayCount = 1; arrayCount < digitArray.length; arrayCount++) { int randNum = (int)(Math.random() * 10); System.out.print(arrayCount + ": "); System.out.print((digitArray[arrayCount] = randNum) + " | "); } //count while (arrayCount < digitArray.length) { counts[0]++; counts[1]++; counts[2]++; counts[3]++; counts[4]++; counts[5]++; counts[6]++; counts[7]++; counts[8]++; counts[9]++; } System.out.println(counts[0]);```
• March 9th, 2014, 03:50 PM
Norm
Re: Counting and Math.random
Can you do this a few steps at a time. There is a lot of code in you post that don't make sense.
1) define an array with 10 slots to hold the counts
2) define a loop that goes around 20 times (later it will be changed to 100)
3) inside the loop, write code to generate a random number from 0 to 9
4) for testing> print out the value of the random number
AND Nothing more
• March 9th, 2014, 04:10 PM
Elyril
Re: Counting and Math.random
Code Java:

```//array int[] counts = new int[10];   //random number loop for(int r = 0; r < 20; r++) { int randNum = (int)(Math.random() * 10); System.out.print(randNum + " "); }```
output:
Code Java:

`8 8 4 3 1 8 4 4 3 2 1 8 9 8 6 3 0 8 1 3`
• March 9th, 2014, 04:13 PM
Norm
Re: Counting and Math.random
now add ONE line of code that uses the random number as an index to the counts array to access one of its elemens.
Increment that element by one.
• March 9th, 2014, 04:20 PM
Elyril
Re: Counting and Math.random
Code Java:

```//array int[] counts = new int[10];   //random number loop for(int r = 0; r < 20; r++) { int randNum = (int)(Math.random() * 10); System.out.print(randNum + " "); counts[0]++; }```
output:
Code Java:

`8 8 4 3 1 8 4 4 3 2 1 8 9 8 6 3 0 8 1 3`
• March 9th, 2014, 04:34 PM
Norm
Re: Counting and Math.random
Why does the code use the value 0 for the index into the array?
I said:
" uses the random number as an index to the counts array "
• March 9th, 2014, 04:38 PM
Elyril
Re: Counting and Math.random
So like this? Since I can't control what numbers show up?
Code Java:

```//array int[] counts = new int[10];   //random number loop for(int r = 0; r < 20; r++) { int randNum = (int)(Math.random() * 10); System.out.print(randNum + " "); counts[randNum]++; }```
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last