EGHDK
EGHDK

Reputation: 18120

Randomizing in Java

I have 4 Strings to represent people and 4 Strings to represent names.

I'm trying to randomize them so that every time I start my application, my four people will have different names, but no one can have the same name during runtime.

Example:

String person_one;
String person_two;
String person_three;
String person_four;

String name_one = "Bob";
String name_two = "Jane";
String name_three = "Tim";
String name_four = "Sara";

Hope this makes some sense.

Upvotes: 5

Views: 301

Answers (2)

Joey
Joey

Reputation: 354356

You can use Collections.shuffle():

List<String> names = new ArrayList<String>();
names.add("Bob");
names.add("Jane");
names.add("Tim");
names.add("Sara");

Collections.shuffle(names);

person_one = names.get(0);
person_two = names.get(1);
person_three = names.get(2);
person_four = names.get(3);

Upvotes: 9

Greg Kramida
Greg Kramida

Reputation: 4224

You can use Collections.shuffle().

Upvotes: 2

Related Questions