Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Random numbers

  • 11-09-2022 7:09pm
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,765 Mod ✭✭✭✭



    I have an arraylist of numbers, I want to add two random numbers from that list to two other lists, I have that working,it always gives me random numbers but the problem is the output is always like this, both indexes are the same number always


    Computer list: [4, 4]
    
    User list: [1, 1]
    


    I would like the 4 and the 1 at index 1 to be different each time, so for example

    Computer list: [4, 3]
    
    User list: [1, 5]
    

    here is the code

    public static void main(String[] args){
             ArrayList<Integer> computerCards = new ArrayList<>();
            ArrayList<Integer> userCards = new ArrayList<>();
            int userChoice =0;
            int computerChoice =0;
            computerChoice = getRandomNumber().get(rand.nextInt(randomNumber.size()));
            userChoice = getRandomNumber().get(rand.nextInt(randomNumber.size()));
            for(int i = 0; i < 2; i++){
                computerCards.add(computerChoice);
                userCards.add(userChoice);
            }
             System.out.println("Computer list: " + computerCards);
             System.out.println("User list: " + userCards);
    }
     
     
    private static ArrayList<Integer> getRandomNumber(){
            ArrayList<Integer> randomNumber = new ArrayList<Integer>();
            randomNumber.add(1);
            randomNumber.add(2);
            randomNumber.add(3);
            randomNumber.add(4);
            randomNumber.add(5);
            randomNumber.add(6);
           return randomNumber;
        } 
    

    How can I achieve this please ?



Comments

  • Registered Users, Registered Users 2 Posts: 6,236 ✭✭✭Idleater


    How do I generate random integers within a specific range in Java?



  • Administrators Posts: 54,091 Admin ✭✭✭✭✭awec


    Like this:

    public static void main(String[] args){
            ArrayList<Integer> computerCards = new ArrayList<>();
            ArrayList<Integer> userCards = new ArrayList<>();
    
            // you only need to create 1 instance of this array. 
            ArrayList<Integer> numberSet = getRandomNumber();
    
            // you may as well just calculate the size once. 
            int numberSetSize = numberSet.size();
    
            for(int i = 0; i < 2; i++) {
                // you call rand.NextInt inside the array to get different values per-index
                computerCards.add(numberSet.get(rand.nextInt(numberSetSize)));
                userCards.add(numberSet.get(rand.nextInt(numberSetSize)));
            }
    
             System.out.println("Computer list: " + computerCards);
             System.out.println("User list: " + userCards);
    }
     
     
    private static ArrayList<Integer> getRandomNumber(){
            ArrayList<Integer> randomNumber = new ArrayList<Integer>();
            randomNumber.add(1);
            randomNumber.add(2);
            randomNumber.add(3);
            randomNumber.add(4);
            randomNumber.add(5);
            randomNumber.add(6);
           return randomNumber;
        } 
    




  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,765 Mod ✭✭✭✭Sephiroth_dude




Advertisement