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
anyItem is a method and the System.out.println call is after your return statement so that won't compile anyway since it is unreachable.
Might want to re-write it like:
import java.util.ArrayList;
import java.util.Random;
public class Catalogue
{
private Random randomGenerator;
private ArrayList<Item> catalogue;
public Catalogue()
{
catalogue = new ArrayList<Item>();
randomGenerator = new Random();
}
public Item anyItem()
{
int index = randomGenerator.nextInt(catalogue.size());
Item item = catalogue.get(index);
System.out.println("Managers choice this week" + item + "our recommendation to you");
return item;
}
}
public static Item getRandomChestItem(List<Item> items) {
return items.get(new Random().nextInt(items.size()));
}
Videos
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)];
}
rand.nextInt(10); gets a random number with no respect to the content of your list.
You need to pass the size of the list there, not literally 10 using myArray.size()
Secondly, if you want your code to do anything other than choose a random element from an empty list, you'll need to call that other method
It's a list, not an array
You never actually created the arrayList, so when you call the array in your showoperations method, it ends up trying to do the operations on an empty array. In addition, I tweaked your calculation for z so that it will give you a number no matter the sizeof the arrayList.
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Operator {
Random rand = new Random();
ArrayList<String> myArray = new ArrayList<String>();
public void createArrayList() {
myArray.add("add");
myArray.add("subtract");
myArray.add("multiply");
myArray.add("divide");
myArray.add("remainder");
myArray.add("greaterthan");
myArray.add("lessthan");
myArray.add("max");
myArray.add("min");
myArray.add("power");
try {
FileReader inFile = new FileReader("data/numbers2.txt");
Scanner scanner = new Scanner(inFile);
String line = scanner.nextLine();
System.out.println(line);
scanner.close();
}
catch (Exception ex) {
ex.printStackTrace();}
}
public void showOperations() {
createArrayList();
int x = (int) Math.floor(Math.random()*10);
int y = (int) Math.floor(Math.random()*10);
int z = rand.nextInt(myArray.size());
System.out.println(x+" "+ myArray.get( z )+" "+ y );
}
}
This is how to select a random element in a list:
List<String> myList = new ArrayList<>();
myList.add("M4A1");
myList.add("Kilo");
myList.add("Grau");
int randomIndex = ThreadLocalRandom.current().nextInt(myList.size());
String randomGun = myList.get(randomIndex);
The value of randomGun will be one of the three guns.
Edit:
To choose randomly a list you can create a Map of lists:
Map<Integer, List<String>> myMap = new HashMap<>();
myMap.put(1, Arrays.asList("M4A1","Kilo","Grau"));
myMap.put(2, Arrays.asList("Merc","Ranger","Commando"));
myMap.put(3, Arrays.asList("50","60","70"));
int randomIndex = ThreadLocalRandom.current().nextInt(myMap.size());
List<String> randomList = myMap.get(randomIndex);
For a simple project is OK, but if you plan to extend this project then I recommend to use classes.
This could be achieved by storing your values in a collection that can be accessed by index, as you are doing with your array. Then you generate a random integer between 0 and your highest index. Take the value from the collection at that index. Voila.