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)
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)];
}
Videos
First you'll have to move the S array to be an instance variable or a static variable, because currently it's local to your main method, and can't be accessed from your get method.
Then you can get a random String this way :
private Random rnd = new Random();
public String get ()
{
return S[rnd.nextInt(S.length)];
}
You can use java.util.Random to generate random things. However, keep in mind that, it is not secure, not really random.
You can get random chars from the array S:
String randomString = "";
Random rand = new Random();
for(int i=0;i<=4;i++)
{
randomString += S[rand.nextInt(S.length())].charAt(0);
}
System.out.println(randomString);
Try:
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
To select two or more strings randomly from an array, I would use a for loop in combination with two generated integers. One random integer to select an element in the string array and the other to determine how many times the for loop runs, selecting an element for each time it loops.
String [] arr = {"A", "B", "C", "D"};
Random random = new Random();
int n = 0;
int e = 0;
//A random integer that is greater than 1 but not larger than arr.length
n = random.nextInt(arr.length - 2 + 1) + 2;
//loops n times selecting a random element from arr each time it does
for(int i = 0; i < n; n++){
e = random.nextInt(arr.length);
System.out.println("Random String selected: " + arr[e]);
}
If I understood you correctly, then I think you can just run
// randomly selects an index from the arr
int select = random.nextInt(arr.length);
// prints out the value at the randomly selected index
System.out.println("Random String selected: " + arr[select]);
again. It will select another random String from the array and print it out.
You can use
Collections.shuffle(Arrays.asList(cards));
You can use this if you want to copy to a new array.
public String[] randome(String[] arr) {
Random rgen = new Random();
String[] randArray = new String[arr.length];
System.arraycopy(arr, 0, randArray, 0, arr.length);
for (int i = 0; i < randArray.length; i++) {
int randIn = rgen.nextInt(randArray.length);
String temp = randArray[i];
randArray[i] = randArray[randIn];
randArray[randIn] = temp;
}
return randArray;
}