Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 31

Thread: Counting and Math.random

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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!
    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
    0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Counting and Math.random

    how would I do that exactly with 100 different arrays? The programs I've seen only do something like
    array[0] = 0
    array[1] = 2
    array[2] = 5
     
    //etc.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Counting and Math.random

    Instead of the numbers: 0,1,2 as the index, use the for loop control variable as the index.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.
    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.
    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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
    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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Elyril (March 8th, 2014)

  10. #9
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.. :/

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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
    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++;

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Counting and Math.random

    Treat the element of an int array as an int variable:
    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
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.
    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
    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.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.
    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);
    			}
    		}

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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]++;
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.
    	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]);

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    Start with these first steps:
    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
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Counting and Math.random

    //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:
    8 8 4 3 1 8 4 4 3 2 1 8 9 8 6 3 0 8 1 3

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Counting and Math.random

    //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:
    8 8 4 3 1 8 4 4 3 2 1 8 9 8 6 3 0 8 1 3

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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 "
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Counting and Math.random

    So like this? Since I can't control what numbers show up?
    //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]++;
    		}

Page 1 of 2 12 LastLast

Similar Threads

  1. Java Math.random() and loop
    By maple1100 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: December 28th, 2012, 12:23 PM
  2. Math.random() with no duplictaes
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 6th, 2012, 07:42 AM
  3. Problems with Math.Random() in a for loop
    By csharp100 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 06:18 PM
  4. Need help understanding math.random method.
    By slashdash in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2012, 05:50 PM
  5. Math.Random()
    By xionyus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2011, 10:22 PM