Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • Randomly choose between two ints


    Wayslili
     Share

    Recommended Posts

    5 hours ago, Wayslili said:

    i dont want to choose between the two numbers, i want it to choose only from the set of two numbers.

    2 ways:

    1) Through an array using the Random class.

    Random random = new Random();
    
    int randomPickedNumber = arrayName(random.nextInt(arrayName));

     

    2) Through Lists using Collections.shuffle();

    ArrayList<Integer> listName = new ArrayList<Integer>();
    
    Collections.shuffle(listName);
    
    int randomPickedNumber = listName.get(0);

     

     

    You can also turn the array into a list using

    Collections.asList(arrayName); 

    or turn the list into an array using

    Arrays.toArray(listName);

     

    Also there's a way to pick between 2 numbers is by chance, like the following:

    int chance = Calculations.random(1,100);
    
    int chosenNumber;
    
    if(chance <=50) chosenNumber = firstInteger;
    
    else chosenNumber = secondInteger;

     

     

     

    hope this helps ;)

    Edited by Defiled
    Link to comment
    Share on other sites

    10 hours ago, Wayslili said:

    i dont want to choose between the two numbers, i want it to choose only from the set of two numbers.

    Sorry, I misunderstood. Defiled has provided some good examples, but to add an additional one. If you only want to have two selection choices:

    private Random random = new Random();
    
    
    int randomOfTwoNumbers = random.nextBoolean() ? 3 : 5); // replace 3 and 5 with the numbers you want 

     

    Edited by Shy
    Typo
    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.