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
  • Randomly choose between two ints


    Wayslili

    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 ;)

    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 

     

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

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