How to Random.choice Equivalent in Java for an Array?

Total Post:412

 1458  View(s)
Rate this:

Here is an example of my random.choice implementation in Python.

available_cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'TeamLeader', 'Rohit', 'Kesharwani', 'Alld']
random_computer_card = random.choice(available_cards)
print random_computer_card

Just wondering what the Java equivalent would be for this piece of code. Obviously, I haven't included the import statement in the sample.

  1. Post:13

    Re: How to Random.choice Equivalent in Java for an Array?

    Convert everything to String and define them in an array like below and access from index by generating random number:

    String[] available_cards = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "TeamLeader", "Rohit", "Kesharwani", "Alld"}; 
    java.util.Random random = new java.util.Random();
    int random_computer_card = random.nextInt(available_cards.length);

      Modified On Apr-06-2018 04:14:34 AM



Enter your email address here always to be updated. We promise not to spam!