Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on [email protected] for help. Thanks :)
Hello All, This is just a friendly reminder to read the Forum Charter where you wish to post before posting in it. :)
Hi all, The AutoSave Draft feature is now disabled across the site. The decision to disable the feature was made via a poll last year. The delay in putting it in place was due to a bug/update issue. This should serve as a reminder to manually save your drafts if you wish to keep them. Thanks, The Boards Team.
Hello all! This is just a quick reminder to 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.

Random numbers

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



    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 Posts: 6,135 ✭✭✭ Idleater


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



  • Administrators Posts: 51,082 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: 78,494 Mod ✭✭✭✭ Sephiroth_dude




Advertisement