Solved Getting a random number

Discussion in 'Plugin Development' started by Forword, Apr 23, 2018.

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

    Forword

    How would you get 2 random numbers that both add up to 100?

    I currently have:

    [/code]
    public static int getRandom(int lower, int upper)
    {
    Random random = new Random();
    return random.nextInt(upper - lower + 1) + lower;
    }
    [/code]

    But that doesn't really work with what I'm trying to get.
     
  2. Offline

    JanTuck

    Do you want two numbers or just 1?


    Sent from my iPhone using Tapatalk
     
  3. Offline

    Forword

    2 numbers
     
  4. Offline

    JanTuck

    So if the random number was 19, the other number would be 81?


    Sent from my iPhone using Tapatalk
     
  5. Offline

    Forword

  6. Offline

    Zombie_Striker

    @Forword
    int upper = Math.random*100;

    int lower = 100-upper;
     
    JanTuck likes this.
  7. Offline

    Forword

    Ok lemme test that @Zombie_Striker

    @Zombie_Striker
    Where would you like me to put that beacause its saying:
    random cannot be resolved or is not a field

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  8. @Forword
    Here is your code:

    Code:
        public static int[] getNumbers(int lower, int upper)
        {
            Random random = new Random();
            int num_1 = random.nextInt(upper - lower + 1) + lower;
            return new int[] {num_1, upper-num_1};
        }
    for instance: If you type
    Code:
    int[] result = getNumbers(0, 100);
    and the num_1 will set to 39, then the num_2 will be (upper-39) = (100-39) = 61
    as result. result[0] = 39 and result[1] = 61.

    //DISCLAIMER// This is an elaborated code of @Zombie_Striker's example.

    I hope this helps you out.
     
    Last edited: Apr 23, 2018
  9. Offline

    Forword

    Thank you very much, that is what I wanted :D

    @DanielTheDev
    EDIT: If I were to use that in 2 lines what would I have to do?

    lore1.add(ChatColor.GREEN + "Success Rate: " + getRandom(1, 50) + ChatColor.GREEN + "%");
    lore1.add(ChatColor.RED + "Destroy Rate: " + getRandom(1, 50) + ChatColor.RED + "%");

    Each line will have a number and
    the numbers have to equal 100 :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 23, 2018
  10. @Forword

    I think you mistunderstood my code.
    The method will create 2 numbers (array).

    This will be the right code for you.
    Code:
    int[] result = getRandom(1, 50);
    
    lore1.add(ChatColor.GREEN + "Success Rate: " + result[0] + ChatColor.GREEN + "%");
    lore1.add(ChatColor.RED + "Destroy Rate: " + result[1] + ChatColor.RED + "%");
    I think this solves your problem :)
     
  11. Offline

    JanTuck

    Random instances are quite wasteful please use ThreadLocalRandom to get over this.

    This function will do the same exact thing.
    Code:
        public static int[] getNumbers(int lower, int upper) {
            int[] returnVal = new int[2];
            returnVal[0] = ThreadLocalRandom.current().nextInt(lower, upper);
            returnVal[1] = upper - returnVal[0];
            return returnVal;
        }
     
  12. Offline

    Forword

    @JanTuck
    For your code, how would it work?

    EDIT: Like when your using it in a message, how would it work?
     
  13. Offline

    JanTuck

    The same way his code worked.

    Code:
    int[] chances = getNumbers(1, 50);
    lore1.add(ChatColor.GREEN + "Success Rate: " + chances[0] + ChatColor.GREEN + "%");
    lore1.add(ChatColor.RED + "Destroy Rate: " + chances[1] + ChatColor.RED + "%");
     
  14. Offline

    Forword

    Alright thanks, Ill test it out in a bit

    @JanTuck, the numbers it gave me were always 57 and 43

    Heres my code:

    Code:
    public static int[] getNumbers(int lower, int upper) {
            int[] returnVal = new int[2];
            returnVal[0] = ThreadLocalRandom.current().nextInt(lower, upper);
            returnVal[1] = upper - returnVal[0];
            return returnVal;
        }
            int[] chances = getNumbers(1, 50);
    
    lore.add(ChatColor.GREEN + "Success Rate: " + chances[0] + ChatColor.GREEN + "%");
    lore.add(ChatColor.RED + "Destroy Rate: " + chances[1] + ChatColor.RED + "%");
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 23, 2018
  15. Offline

    JanTuck

    It can’t be unless you made the upper 100


    Sent from my iPhone using Tapatalk
     
  16. Offline

    timtower Administrator Administrator Moderator

    @Forword code blocks are [code] <code here> [/code]
    Not [/code] <code here> [/code]
     
  17. Offline

    Forword

    @JanTuck
    When I make the upper 100, it still is 57, 43

    EDIT: I need those numbers to be random everytime
    To elaborate more on that, whenever I try to get 2 different numbers,
    they always turned out be to 57 and 43
     
    Last edited: Apr 23, 2018
  18. Offline

    JanTuck

    Then get another pair of random numbers each time and update the lore
     
  19. Offline

    Forword

    @JanTuck
    Is there an efficient way to constantly get 2 different numbers which add up to 100?
    Ex.
    50, 50 | 25,75
    Those numbers will constantly be changing. The code which you has provided me with, only get a combination of 2 of the exact same numbers each time. I'm trying to use this for a success and destroy rate for a custom enchantment plugin.

    Thanks, Forword :D
     
  20. @Forword
    You could generate 1 number between 1 and 100 and then to get the second number do
    int secondNum = 100 - randNum;
     
  21. Offline

    Forword

    @Blackwing_Forged
    if you mind, can you make a piece of code for that?
    I’m only asking this because whenever I try doing it, it never works.
     
  22. @Forword
    Can you show me your current code
     
  23. Offline

    Forword

    Code:
    public static int getRandom() {
                Random random = new Random();
                int Failure = random.nextInt(1-100);
                int Success = 100 - Failure;
                return Success;
            }
    
    How do I get failure number?
     
  24. Code:
    public static int[] getRandom() {
                int Failure = ThreadLocalRandom.current().nextInt(1-100);
                int Success = 100 - Failure;
                return new int[] {Success, Failure};
      }
    
    int[] both = getRandom();
    int success = both[0];
    int failure= both[1];
    
     
  25. Offline

    Forword

    @DanielTheDev

    Code:
    package me.forword.server.commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    import io.netty.util.internal.ThreadLocalRandom;
    
    public class Test implements CommandExecutor {
    
        public static int[] getRandom() {
            int Failure = ThreadLocalRandom.current().nextInt(1-100);
            int Success = 100 - Failure;
            return new int[] {Success, Failure};
        }
        int[] both = getRandom();
        int success = both[0];
        int failure= both[1];
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            sender.sendMessage(ChatColor.GREEN + "Success Rate:" + both[0]);
            sender.sendMessage(ChatColor.RED + "Failure Rate:" + both[1]);
            return true;
           
           
           
        }
    }
    
    Is there a reason why that wont work?
     
  26. Offline

    timtower Administrator Administrator Moderator

    @Forword Use that code in the onCommand, not outside where it only gets called once.
     
  27. Offline

    JanTuck

    Why are you running NextInt on -99?
     
  28. Offline

    Forword

    @timtower
    it doesnt work when i put it inside the onCommand
     
  29. Offline

    JanTuck

    Because the method needs to be outside, the getter inside.
     
  30. Offline

    Forword

    @JanTuck
    can you think of another way to get 2 numbers that will always reach 100?
     
Thread Status:
Not open for further replies.

Share This Page