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)];
}
java - Random element from string array - Stack Overflow
java - Random string from string array list - Stack Overflow
java - Pick a random element from a string array? - Stack Overflow
java - How to get a random string from an array of strings? - Stack Overflow
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
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);
public static int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
You can use the Random generator to generate a random index and return the element at that index:
//initialization
Random generator = new Random();
int randomIndex = generator.nextInt(myArray.length);
return myArray[randomIndex];
Two things you have to change:
- currentRoom should be String Array
Printing string array should be done using Arrays.toString
String[] currentRoom; String [][] rooms = {{"Start", "Treasure Room1"}, {"Goblin Home1", "Spider Nest1"}}; Random rand = new Random(); currentRoom = rooms [rand.nextInt(rooms.length)]; System.out.println(Arrays.toString(currentRoom));
Sample Output
[Goblin Home1, Spider Nest1]
Hope this helps!
Why do you need a 2D array of strings? I think you can manage with a 1D array instead.
String currentRoom;
String[] rooms = {"Start", "Treasure Room1", "Goblin Home1", "Spider Nest1"};
Random rand = new Random();
currentRoom = rooms [rand.nextInt( rooms.length)];
System.out.println(currentRoom);
I am a beginner when it comes to Java, but I am trying to create a program that will output a complete Fantasy Football lineup at random.
My first step was to create an ArrayList of all the teams in the NFL (not sure if this was the most appropriate approach).
With the ArrayList filled, how to I then print a random String from it? In other words, I would like the program (at this point) to simply output a random NFL team. I realize that this is not a complete fantasy lineup.
http://pastebin.com/181vJPKE
Edit: I am not sure how to embed from pastebin. Help?
You mean...
if(Color.equals("Black")) {
// then do this
} else if(Color.equals("Red"){
// then do this
}
or even (In Java >= 1.7)
switch(Color) {
case "Black":
// then do this
break;
case "Red":
// then do this
break;
}
Color should not be capitalized, since that can be a Java class name.
For this purpose you can use a ternary operator (shortened if-else):
String color = ( rnum[random].compareTo("Black") == 0 ? ** do something here ** : ** do something if the color is not "Black" ** );
or:
String color = "";
if(rnum[random].compareTo("Black") == 0) {
// do stuff
}
else {
// it's not black. do other stuff.
}
With red and green, just replace "Black" with "Red", or "Green".