Results 1 to 16 of 16
  1. #1
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Cool 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 =)

    lets say you had:

    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!
    Last edited by Samgetsmoney; 02-17-2009 at 05:09 AM.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    And they are called at random.
    what you mean saying that? what method calls them or what realizes the effect? post some code )

  3. #3
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    There updated hows that?

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Heads spinning ...

    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...
    Java 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    T get a random int, try this:
    Java 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
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    and where is the random limit? or it is non-limited one?
    Math.random(n);

  7. #7
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

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

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    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
    • read data form ArrayList

    Something like (pseudocode)...

    random niumber = 57
    ArrayList [57] = add.Array[57]++

    Make sense?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    Samgetsmoney is offline Member
    Join Date
    Feb 2009
    Posts
    32
    Rep Power
    0

    Default

    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 {
    cases.add(50, 1);
    }

  10. #10
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Java 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
    Last edited by MK12; 02-17-2009 at 11:35 PM. Reason: Forgot Generics
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Close , but I don't think it would work...
    Java 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    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
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    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:
    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    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
    Last edited by MK12; 02-18-2009 at 03:47 AM.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  16. #16
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Ohh ok.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. to get count value as a variable
    By arunkumarinfo in forum JDBC
    Replies: 2
    Last Post: 03-30-2009, 01:32 AM
  2. how we can get the count value in a variable
    By arunkumarinfo in forum JDBC
    Replies: 4
    Last Post: 01-23-2009, 09:39 PM
  3. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 08:49 AM
  4. how to count 2 inserts together?
    By kim85 in forum New To Java
    Replies: 0
    Last Post: 01-20-2008, 11:25 AM
  5. making a count down timer using java
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •