Wayslili 0 Share Posted March 12, 2020 How would i get it to randomly choose between two integers? For example, i want it to randomly to choose between either 3 or 5. Link to comment Share on other sites More sharing options...
Shy 14 Share Posted March 12, 2020 https://dreambot.org/javadocs/org/dreambot/api/methods/Calculations.html#random-int-int- Link to comment Share on other sites More sharing options...
Wayslili 0 Author Share Posted March 12, 2020 8 minutes ago, Shy said: https://dreambot.org/javadocs/org/dreambot/api/methods/Calculations.html#random-int-int- i dont want to choose between the two numbers, i want it to choose only from the set of two numbers. Link to comment Share on other sites More sharing options...
Defiled 423 Share Posted March 12, 2020 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 More sharing options...
Defiled 423 Share Posted March 12, 2020 1 hour ago, Infidel said: Nice example @Defiled ty ❤ Link to comment Share on other sites More sharing options...
Shy 14 Share Posted March 12, 2020 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.