# Making a count..

• 02-17-2009, 05:39 AM
Samgetsmoney
Making a count..
Gah its hard to explain..

but lets say...

There is:

case 1:
break;

case 2:
break;

case 3:
break;

And they are called at random.
I want to try and see wich is called the most.
Ok ok sorry i dont know what term to use say i say "call"
but i mean like
case(random(3));
void case(int n) {
switch (n) {
case 1:
break;

Lets say the random comes up with 1.
Thats what i mean by calling on 1 lol sorry.

Like a simple way i had some ideas but dont know how to do them..
And i dont want some long solution like if (2 > 1)
because there will be over 100 cases not just 3 ;)
Thanks and if i need to explain something more just tell me.

And also theres a second part..

I want to be able to do something like

void case(int num) {
switch (num) {
case 0:
return;

case 1:
return;

case 2:
return;

because if you have break; it doesnt stop the void so it checks through all the rest of them
wich is like a waste i guess. can i do that or can you not use return with cases?

And a part 3 sorry =)

switch (n) {

case 1:
i = a;
continue;

case 2:
i = b;
continue;
}
if (i == a) {
//blah blah
}

If case 1 were true would it skip case 2 and go straight to the if statement or check if case 2 is true?

Thanks everyone!
• 02-17-2009, 05:43 AM
Webuser
Quote:

And they are called at random.
what you mean saying that? what method calls them or what realizes the effect? post some code )
• 02-17-2009, 05:46 AM
Samgetsmoney
There updated hows that?
• 02-17-2009, 02:18 PM
CJSLMAN
Sam... it's very difficult to understand what you want, but I'll try...
part 1:
You what to have a counter for many time each case is randomly picked?
Just add a counter varialble to each case section...
Code:

```int countCase1 = 0; . . . switch... case 1: countCase1++; break;```
Part 2:
Don't call your method "Case". Call it something that has meaning. I don't know what the problem is here. The break command breaks out of the switch statement (it doesn't go through the rest of the case statements). The void in the method declaration indicates that the method doesn't return any values. I don't any relation between the break statement and the void.

Part 3:
Case1 is an assigment. THere is no true or false in your case statement. Try it out. Use continue and break and see what happens.

Luck,
CJLS
• 02-17-2009, 02:50 PM
MK12
T get a random int, try this:
Code:

`int rand = (int)(Math.random() * a) + b;`
a: How high you want it to go, if its 40, then it'll be a number between 0 and 40.
b: To shift it, If you want it between 1 and 40, then a would be 39 and b would be 1.
For counting, you could have an array, or a separate counter for each.
-MK12
• 02-17-2009, 07:55 PM
Webuser
and where is the random limit? or it is non-limited one?
Quote:

Math.random(n);
• 02-17-2009, 10:28 PM
Samgetsmoney
CJSLMAN gets what im saying for part 1
i need a count like that but something more complex like an arraylist or something is what i heard...
Like 100's of cases so adding 100 ints and 100 case++; isnt going to be very organized..
So i was thinking all the case numbers get added to the list then each time a particular case is picked with random it adds one count to the case number..
• 02-17-2009, 10:47 PM
CJSLMAN
Do you know how many random numbers you will be generating? 200? 500? I definitely agree that using switch-case would not be wise. I have another idea....
Instead of switch-case just use an ArrayList:
• start loop
• Generate a random number
• assign a number to that array element (i++)
• stop loop

Something like (pseudocode)...

random niumber = 57

Make sense?

Luck,
CJSL
• 02-17-2009, 10:53 PM
Samgetsmoney
Im looking for something like (sorry if i write this wrong im new)
ArrayList cases = new ArrayList;

random(100);

lets say 50 gets picked
case 50:
if (cases.contains(50)) {
cases(50)++;
} else {
}
• 02-18-2009, 12:26 AM
MK12
Code:

```ArrayList<Integer> list = new ArrayList<Integer>(); ArrayList<Integer> counter = new ArrayList<Integer>(); for(int i = 0; i < NUM; i++) { // replace NUM with how many times to do it     Integer rand = new Integer((int)(Math.random() * 9) + 1); // 1 -10     if(list.contains(rand)) counter.set(list.indexOf(rand), list.indexOf(rand) + 1)     else {           list.add(rand);     } } list.trimToSize(); counter.trimToSize(); for(int i = 0; i < list.size(); i++) {     System.out.println(Occurrences of " + list.get(i) + ": " + counter.get(i)); }```
Would that work? I think but I'm not positive whether the counter and the number will be in the same locations in their respective arrays (wouldn't work if they weren't) try it out and see.
-MK12
• 02-18-2009, 12:44 AM
CJSLMAN
Close , but I don't think it would work...
Code:

`if(list.contains(rand))`
This would return true if the element (rand) existed, but you need to know if the index is populated or not... right?
There can be various solutions... what I did was:
• Create an arraylist and fill it with zeros (with for loop)
• start loop
• generate random number (randNum)
• randArray.set(randNum,randArray.get(randNum)+1);
• end loop

Luck,
CJSL
• 02-18-2009, 12:47 AM
MK12
Your talking about whether there's space in the counter arraylist when i set the value, right? I could just add it also when the else is executed, or ensureCapacity, couldn't I (or Him if he uses that).
-MK12
• 02-18-2009, 12:59 AM
CJSLMAN
hhhmmm... the thing is that you don't need two arraylists. You can do it with one... for example
• generate arraylist with size = 200 and fill with zeros
• start loop
• generated random number = 57 (i use the Random class ... I don't like the math one)
• set the arraylist index (randNum) with the number it has (0) plus one -> randArray.set(randNum,randArray.get(randNum)+1);
• end loop
• print the arraylist: a sample output of this using an array size = 10:
Quote:

randArray[0]: 1
randArray[1]: 1
randArray[2]: 0
randArray[3]: 0
randArray[4]: 1
randArray[5]: 0
randArray[6]: 3
randArray[7]: 1
randArray[8]: 1
randArray[9]: 2
randArray[10]: 1
Luck,
CJSL
• 02-18-2009, 02:04 AM
MK12
Oh ya that makes more sense.. but why the + 1? then random num 50 would be in index 51 in the zero-starting list right?
EDIT: And what's wrong with Math.random()? I know a generator is easier when making many numbers but I'm only doing it once in this situation (which loops).
-MK12
• 02-18-2009, 02:09 AM
CJSLMAN
Because your not saving the random number... the random number is the index in the ArrayList. Your saving the times that the random number is generated, hence the "+1" (I tried it get to accept the ++, but didn't have any luck).

CJSL
• 02-18-2009, 02:44 PM
MK12
Ohh ok.
-MK12