need to count the occurances of randomly generated numbers using array

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1936 Views - Last Post: 09 October 2012 - 07:29 AM Rate Topic: -----

#1 javanewbie101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-July 12

need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 01:31 AM

I need my program to generate 50 random integers in the range 0 to 50(0 and 50 are included) and count the occurances
I have this so far:

import java.util.Random;


public class P2 {
	
	public static void main(String[] args) {
		
		System.out.println("\t" + "\t" + "\t" + "   random integers");
		
		Random num = new Random();
		int count[] = new int[51];
		
		for(int i = 0; i < 50; i++) {
			++count[num.nextInt(50)];
			
		}
		
		for(int j = 0; j < count.length; j++) {
			System.out.println("integer:" + j + "\t" + "count:" + count[j] + "\t" + "\t" + num.nextInt(50));
			
		}
	}
}


However the counts are not correct. Below are three columns: Ineger, Count, and random integer. For Integer:49 it says Count:2 but if you look at the random integers column 49 doesn't appear at all! What am I missing here.

			   random integers
integer:0	count:1		40
integer:1	count:0		3
integer:2	count:0		45
integer:3	count:0		40
integer:4	count:0		8
integer:5	count:1		0
integer:6	count:2		43
integer:7	count:0		28
integer:8	count:0		36
integer:9	count:0		21
integer:10	count:1		40
integer:11	count:0		34
integer:12	count:0		41
integer:13	count:2		10
integer:14	count:1		24
integer:15	count:2		7
integer:16	count:1		19
integer:17	count:1		23
integer:18	count:1		20
integer:19	count:1		15
integer:20	count:1		37
integer:21	count:1		27
integer:22	count:1		41
integer:23	count:0		20
integer:24	count:2		35
integer:25	count:1		32
integer:26	count:2		8
integer:27	count:0		28
integer:28	count:0		38
integer:29	count:1		42
integer:30	count:1		15
integer:31	count:0		48
integer:32	count:1		3
integer:33	count:2		33
integer:34	count:2		19
integer:35	count:4		33
integer:36	count:1		48
integer:37	count:1		8
integer:38	count:1		3
integer:39	count:1		7
integer:40	count:3		36
integer:41	count:2		35
integer:42	count:1		43
integer:43	count:0		11
integer:44	count:1		25
integer:45	count:1		38
integer:46	count:2		4
integer:47	count:0		44
integer:48	count:1		38
integer:49	count:2		38
integer:50	count:0		26

This post has been edited by Atli: 09 October 2012 - 01:32 AM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: need to count the occurances of randomly generated numbers using array

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 01:50 AM

This should help you out Here If you have any problems with it, let us know. Make sure you learn whats happening though.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1372
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 01:53 AM

What?

Why are you generating a random number when you are printing out the results?
I think you are confusing yourself

System.out.println("integer:" + j + "\t" + "count:" + count[j]);


Was This Post Helpful? 0
  • +
  • -

#4 javanewbie101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-July 12

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 02:03 AM

I WANTED TO CHECK TO SEE IF IT WAS RUNNING CORRECTLY BY COUNTING THE NUMBERS MYSELF
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 02:11 AM

No need for the caps lock. That link I provided was a question asked on here which was nearly identical to your question. You can ammend it to fit your needs. All you need to do is change the range of random numbers.
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1372
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 02:11 AM

View Postjavanewbie101, on 09 October 2012 - 09:03 AM, said:

I WANTED TO CHECK TO SEE IF IT WAS RUNNING CORRECTLY BY COUNTING THE NUMBERS MYSELF


Then dont generate random numbers when printing out the results, instead print out the random generated number in your first loop
Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 02:24 AM

View Postjavanewbie101, on 09 October 2012 - 02:33 PM, said:

I WANTED TO CHECK TO SEE IF IT WAS RUNNING CORRECTLY BY COUNTING THE NUMBERS MYSELF


Please don't use caps lock. People are doing their best and trying to help you out here.It is not polite to yell at them.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#8 javanewbie101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-July 12

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 02:32 AM

im not using caplock to yell at people i was doing something that required caplock and i didnt take it off.
Was This Post Helpful? 0
  • +
  • -

#9 javanewbie101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-July 12

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 03:42 AM

does num.nextInt(50) give me the range 0 to 50?
Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1372
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 03:50 AM

View Postjavanewbie101, on 09 October 2012 - 10:42 AM, said:

does num.nextInt(50) give me the range 0 to 50?


nope it gives you 0-49 the 50 is exclusive
Was This Post Helpful? 0
  • +
  • -

#11 javanewbie101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-July 12

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 03:54 AM

that must be why the counts are off, correct? How do i get 0 - 50 inclusive?
Was This Post Helpful? 0
  • +
  • -

#12 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 03:59 AM

View Postjavanewbie101, on 09 October 2012 - 04:24 PM, said:

that must be why the counts are off, correct? How do i get 0 - 50 inclusive?


you could try changing it to nextInt(51). In that case, you will get 0-50 :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#13 javanewbie101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-July 12

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 04:05 AM

import java.util.Random;


public class P2 {



public static void main(String[] args) {


Random num = new Random();


int count[] = new int[51];


for(int i = 0; i < 50; i++) {

++count[num.nextInt(51)];
System.out.println(num.nextInt(51));

}


for(int j = 0; j < count.length; j++) {

System.out.println("integer:" + j + "\t" + "count:" + count[j]);


}

}

}

The run looks like this:

38
8
33
0
6
38
47
16
14
36
29
32
13
11
29
41
44
16
10
25
40
48
8
41
12
29
28
49
1
14
8
41
45
22
27
10
37
33
22
49
8
22
26
9
3
30
31
4
50
50
integer:0 count:3
integer:1 count:1
integer:2 count:1
integer:3 count:1
integer:4 count:1
integer:5 count:1
integer:6 count:2
integer:7 count:1
integer:8 count:1
integer:9 count:0
integer:10 count:2
integer:11 count:1
integer:12 count:2
integer:13 count:1
integer:14 count:1
integer:15 count:2
integer:16 count:0
integer:17 count:2
integer:18 count:1
integer:19 count:0
integer:20 count:1
integer:21 count:3
integer:22 count:2
integer:23 count:0
integer:24 count:0
integer:25 count:1
integer:26 count:0
integer:27 count:1
integer:28 count:1
integer:29 count:3
integer:30 count:0
integer:31 count:0
integer:32 count:2
integer:33 count:0
integer:34 count:1
integer:35 count:0
integer:36 count:0
integer:37 count:0
integer:38 count:0
integer:39 count:1
integer:40 count:0
integer:41 count:1
integer:42 count:2
integer:43 count:1
integer:44 count:1
integer:45 count:1
integer:46 count:1
integer:47 count:1
integer:48 count:2
integer:49 count:0
integer:50 count:0

This is strange because 50 appear twice but the count for 50 says 0. I have no idea what's wrong.
Was This Post Helpful? 0
  • +
  • -

#14 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 04:17 AM

Not only that, certain numbers don't match with the count at all, for eg. is displayed only once but the count is 3. Something in your code is not totaling correctly. We need to find out what is the correction we need to do in your code.

regards,
Raghav

Well, guess what, there is no condition in your code which actually adds the counts of the numbers. So, it is doing the count randomly.
Was This Post Helpful? 0
  • +
  • -

#15 javanewbie101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-July 12

Re: need to count the occurances of randomly generated numbers using array

Posted 09 October 2012 - 04:25 AM

i'm so lost :(
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2