Solved [UNSOLVED] CHANCES

Discussion in 'Plugin Development' started by PickNChew, May 9, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    PickNChew

    I've looked at multiple threads on the forums but none gave me answers. What I want to do is to check the chances listed in a config. It'd be like 1/10, 1/100, 1/1000 etc. My brain can't process how to do this... So basically, you have multiple things that the it could be. But I don't want to generate a number one after another. I want it so that 1/10, 1/100, etc. will have equal chances of happening. I don't really know if this made sense but if you need further explaining I will.
     
  2. Offline

    DxDy

    I think further explanation would be useful.

    As far as I understand you want different actions to happen with a different propability? The basis here would be the Java Random Number Generator.
    Code:java
    1. Random random = new Random();
    2. double value = random.nextDouble();
    3.  
    4. // border is the propability (10% = 10/100, ...)
    5. if(value < border){
    6. // Do the action
    7. }

    That would be the basis for anything else.
     
  3. Offline

    PickNChew

    DxDy Yeah, I know how to create a random number. I'm trying to get the chance out of something like this:
    Code:
    Something:
      Chance: 1000
    Something1:
      Chance: 10000
    Something2:
      Chance: 100
    So then it would only do one of those things.

    Any help is appreciated. Sorry if bumping isn't allowed ;?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Offline

    GeorgeeeHD

    Get the number from the config then this....

    Code:java
    1. Random random = new Random();
    2. int intFromConfig = getNumFromConfig;
    3. int rand = random.nextInt(intFromConfig); //Number between 0 and config chance number


    then do what DxDy said
     
  5. Offline

    PickNChew

    I have multiple chances. If I were to make a for loop, there could be a chance that more than 1 is == 1.
     
  6. Offline

    minoneer

    1. Save all Chances from config
    2. Sum them up
    3. Get a random Value between 0 and the sum
    4. Sum them up again until the sum is > than your random number

    There might be more efficient algorythms, but for most uses this should be fine.
     
  7. Offline

    Shadow_Parallax

    1. Create a boolean, set it to false.
    2. Create a for loop to iterate through all your values
    3. If value x (from the for loop which is loaded from your config file) is smaller than your chance value set boolean to true
    4. In the for loop (at the end) check if the boolean is true, if so
    Code:java
    1. break;


    In the end it'd look something like this
    Code:java
    1. Random r = new Random();
    2. boolean hasHappened = false;
    3.  
    4. for(int i = 0; i < values.length; i++){
    5. if(r.nextInt() < values[ i ]){
    6. hasHappened = true;
    7. }
    8. if(hasHappened){
    9. break;
    10. }
    11. }


    EDIT: More efficient way
    Code:java
    1.  
    2. Random r = new Random();
    3. boolean hasHappened = false;
    4.  
    5. for(int i = 0; i < values.length; i++){
    6. if(r.nextInt() < values[ i ]){
    7. // Do stuff
    8. break;
    9. }
    10. }
     
    PickNChew likes this.
  8. Offline

    PickNChew

    Yep figured it out this morning. Thanks ;)
     
  9. Offline

    Shadow_Parallax


    No problem :p
     
  10. Offline

    PickNChew

    Shadow_Parallax I seem to be having some trouble. I'm trying to create an infinite for loop so I can break it later. But I need to loop through my config like this:
    Code:
    for(String key : getConfig().getConfigurationSection("Rewards").getKeys(false))
    I've tried doing this in the for loop.
    Code:
    if (lucky == false) {
        continue;
    }
     
    if (lucky == true) {
        // Do stuff
        break;
    }
    continue; doesn't seem to make it loop again?

    Anyone please help me. I don't want to wait 24 hours to get a response on repeating a loop.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  11. Offline

    Shadow_Parallax

    PickNChew You don't need to do anything to make it continue. Just have a condition on which it can exit, like you have. So remove the if (lucky == false) bit.
     
  12. Offline

    PickNChew

    Yes, but I need atleast one to be selected. So if none of them == 1 one of them has to be 1.

    The continue; bit was to try to make it loop again.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  13. Offline

    Shadow_Parallax

    PickNChew After the loop check if lucky is still false and then just set it to true. It's easier for me to help if you post your whole method here though. :p
     
  14. Offline

    PickNChew

    I don't want to set it to true. I want it to do it by Chance. Basically, what I'm doing is generating random numbers from 1 - something in the config. Until lucky == true it will keep looping that's what I'm trying to do.
     
  15. Offline

    Bobit

    Use a while loop?
    Code:java
    1. lucky=false;
    2. while(lucky){
    3. //keep repeating this
    4. }


    This is really basic...
     
  16. Offline

    PickNChew

    I've tried using a while loop with a for loop nested into it. It crashes my server

    Nevermind, I've solved it myself.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  17. Offline

    Shadow_Parallax


    That code would never execute.
     
  18. Offline

    Bobit

    Shadow_Parallax

    Context:
    "Until lucky == true it will keep looping that's what I'm trying to do."
     
  19. Offline

    Shadow_Parallax


    Yeah. So it'll never execute.
    while(lucky) <--- Lucky is false, while loop will never execute.
    while(!lucky) <--- Lucky is false, so condition is true, while loop will execute.
     
  20. Offline

    Bobit

Thread Status:
Not open for further replies.

Share This Page