Badr Hari
Badr Hari

Reputation: 8384

Random string from string array list

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();

Upvotes: 18

Views: 68833

Answers (8)

Isaac King
Isaac King

Reputation: 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]);

}

Upvotes: -1

Jimale Abdi
Jimale Abdi

Reputation: 2854

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

Upvotes: 5

Shai Alon
Shai Alon

Reputation: 990

Here's a solution in 1 line:

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

Upvotes: 0

Prthbiraj Chouhan
Prthbiraj Chouhan

Reputation: 41

static String getRandomString(){
        int r = (int) (Math.random()*5);
        String name = new String [] {"India","USA","UK","Russia"}[r];
        return name;
    }

Upvotes: 2

Mike H
Mike H

Reputation: 11

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]).

Upvotes: 1

roni bar yanai
roni bar yanai

Reputation: 1492

list[Math.floor(r.nextFloat()*5.99)]

Upvotes: 0

BRK
BRK

Reputation: 140

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.

Upvotes: 0

Burleigh Bear
Burleigh Bear

Reputation: 3314

Try:

list[r.nextInt(list.length)];

Upvotes: 38

Related Questions