Try:
list[r.nextInt(list.length)];
Answer from Burleigh Bear on Stack OverflowTry:
list[r.nextInt(list.length)];
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
Hello I have a list.
String[] boysNames = {"Bob", "Adam", "Tom", "Brandon"};
What is some code that would pick one string from this list?
public static void main(String[] args) throws InterruptedException {
List<String> my_words = new LinkedList<String>();
my_words.add("1153 3494 9509 2 0 0 0 0");
my_words.add("1153 3487 9509 2 0 0 0 0");
my_words.add("1153 3491 9525 2 0 0 0 0");
my_words.add("1153 3464 9513 2 0 0 0 0");
Random rand = new Random();
while (true) {
int choice = rand.nextInt(my_words.size());
System.out.println("Choice = " + my_words.get(choice));
Thread.sleep(1000);
int replaceTo = rand.nextInt(my_words.size());
System.out.println("Replace to = " + my_words.get(replaceTo));
my_words.set(choice, my_words.get(replaceTo));
}
}
If you have a list/array of data and you want to select a random element from the list. The easiest would probably be to generate a random number with the Math.random (http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html) function which is between 0 and the count of your list/array.
You can then create a Thread that runs forever and sleeps for 7200 seconds between executions that generates a new random number and replaces the old variable.
Just watch out for concurrency issues when using Multi-Threading, have a read at http://download.oracle.com/javase/tutorial/essential/concurrency/.
Update (Example):
Java has a list that can be used to add, and remove data as you want. Data can then by extracted by giving the list the index (number) where the data is located in the list.
So you would be creating a list, then generating a random number in the list's range (0 to the size of the list as the max). And then extracting the data from the list by giving the list your random index. A example would be:
List<String> my_words = new LinkedList<String>();
my_words.add("1153 3494 9509 2 0 0 0 0");
my_words.add("1153 3487 9509 2 0 0 0 0");
my_words.add("1153 3491 9525 2 0 0 0 0");
my_words.add("1153 3464 9513 2 0 0 0 0");
//Maybe a loop to load all your strings here...
Random random = new Random(); //Create random class object
int randomNumber = random.nextInt(my_words.size()); //Generate a random number (index) with the size of the list being the maximum
System.out.println(my_words.get(randomNumber)); //Print out the random word
Hope this makes a bit more sense, and on second thought the Random class in java.util . Would be easier to rap your head around.
Random r = new Random();
creates a pseudo-random list of values. To get other results with each run of the program use:
Random r = new Random(System.currentTimeMillis());
instead.
You're asking to select a random value from a list, and you mention wanting to "reset" after each selection. This is commonly known as "sampling with replacement" meaning that when you select a sample you don't change the probability that you'll select it again the next time. Imagine a drawing cards from a deck - with replacement you put the card back and reshuffle, without replacement you set it aside and draw from the (now smaller) deck.
With-replacement sampling is easy to implement in Java, simply use Random.nextInt(int) to select a random value between 0 and the size of your list. The returned integer is the index to get from your list.
Without-replacement sampling is a little tricker (or requires a different data structure) since you have to either modify the original list or store the set of previously-seen values. which you should prefer depends on how many values you'll need to select.
Several people have suggested Collections.shuffle() which also works, but is more invasive than simply generating a random index. Random.nextInt() is O(1) and doesn't modify your list in any way. Collections.shuffle() has to iterate over the entire list, taking O(n log n) time, and modifies your list. It's useful if you need to select many values from the list, or want without-replacement selection (in which case you shuffle once, then simply iterate over the list).
Use the Random.nextInt(int) method:
final String[] proper_noun = {"Fred", "Jane", "Richard Nixon", "Miss America"};
Random random = new Random();
int index = random.nextInt(proper_noun.length);
System.out.println(proper_noun[index]);
This code is not completely safe: one time out of four it'll choose Richard Nixon.
To quote a documentation Random.nextInt(int):
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
In your case passing an array length to the nextInt will do the trick - you'll get the random array index in the range [0; your_array.length)
if you use List instead of arrays you can create simple generic method which get you random element from any list:
public static <T> T getRandom(List<T> list)
{
Random random = new Random();
return list.get(random.nextInt(list.size()));
}
if you want to stay with arrays, you can still have your generic method, but it will looks bit different
public static <T> T getRandom(T[] list)
{
Random random = new Random();
return list[random.nextInt(list.length)];
}