16

I have list of countries in my array, I would like to pick random country from the list (using random probably?), but I haven't found out the answer myself...

This is what I have so far:

String[] list = {"Finland", "Russia", "Latvia", "Lithuania", "Poland"};
Random r = new Random();
Badr Hari
  • 8,114
  • 18
  • 67
  • 100

8 Answers8

37

Try:

list[r.nextInt(list.length)];
Burleigh Bear
  • 3,274
  • 22
  • 32
  • @roni: Burleigh Bear is correct, the value will never be list.length. As per the Java documentation: "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value **(exclusive)**" – TaylorP Jul 17 '11 at 22:03
  • @Burleigh Bear Can you please tell me how can I choose a random string from that array and also I want some other string every time. I mean if I don't want to repeat the string value what's modification I need in this code . – Kunu Mar 09 '14 at 14:03
  • The usual way to do that is to permute the list (i.e. do a shuffle - you can look up Knuth's algorithm for this). Then you can just iterate through the list to return each item successively. – Burleigh Bear Mar 10 '14 at 06:39
5

The accepted answers is not working for me the solution worked for me is

List<String> myList = Arrays.asList("A", "B", "C", "D");

Suppose you have this above ArrayList and you want to randomize it

    Random r = new Random();

    int randomitem = r.nextInt(myList.size());
    String randomElement = myList.get(randomitem);

If you print this randomElement variable you will get random string from your ArrayList

Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33
2
static String getRandomString(){
        int r = (int) (Math.random()*5);
        String name = new String [] {"India","USA","UK","Russia"}[r];
        return name;
    }
1

Ran into a similar problem with pulling a random string from a string array. Found this to work fairly well, I applied it to a button action so with every click a random is drawn(I found with any array size there are multiple instances of the same string being drawn consecutively throughout):

import java.util.*;
import java.util.Random.*;

class Countries {
    public Random g2 = new Random();
    public String[] list = new String[] { "Finland", "Russia",
            "Latvia", "Lithuania", "Poland" };
    String random2;
}

// Applied to a button action:
int INDEXn = g2.nextInt(list.length);
for (int i2 = 0; i2 < INDEXn; i2++) {
    random2 = (String) (list[INDEXn]);
}
System.out.println(random2 + '\n');

The g2 random that is being used by INDEXn is calling a random integer, in this case the defined strings are turned into integer values, from the String array list. The for-loop is cycling through the String array one time. The random2 string is converting the chosen integer from INDEXn to the appropriate string variable in (list[INDEXn]).

Mike H
  • 11
  • 4
0
private void pickText(){ 
    textview textView1= (TextView) findViewById(R.Id.textView1)
    Random eventPicker = new Randorn();
    randomN = eventPicker.nextInt(3) +1; 

    switch (randomN){ 
        case 1: Intent a textview1.setText(StringOne);
        break; 

        case 2: textview1.setText(StringTwo);
        break; 

I typed it from my phone there's prob syntax errors but it works.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
BRK
  • 140
  • 1
  • 2
  • 9
0

Here's a solution in 1 line:

String country = new String[] {"Finland", "Russia", "Latvia", "Lithuania", "Poland"}[(int)(Math.random()*5)];

Shai Alon
  • 969
  • 13
  • 21
0
list[Math.floor(r.nextFloat()*5.99)]
Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
roni bar yanai
  • 1,492
  • 10
  • 12
-1

import java.util.Random; public static void main (String [] args){

// For this code, we are trying to pic a random day from days

String [] days = {"Sunday","Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday"};

Random rand = new Random();

int Rand_item = rand.nextInt(days.length);

System.out.println(days[Rand_item]);

}

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 01 '21 at 15:02