I have to create a class that will pick partners at random for my class tomorrow, out of the people that are in it.

What I have so far is simple enough...

import java.util.*;
import java.util.Random;
public class GetPartners {

public GetPartners() {
}

public void getPartner() {
String[] names = { "Lukas", "Michele", "Megan", "Lindsey", "Victoria", "Andrew", "Gino", "Andrew", "Chris", "Steve", "Ryan", "Dan", "John", "Andre", "Pat", "Ruta", "Vince" } ;
Random r = new Random();
x = r.nextInt(18);
y = r.nextInt(18);
System.out.println(names[y]+" "+names[x]);

}

private int x;
private int y;

}

And in a tester class, it picks the partners at random and all, but I don't know what to do to make everyone get a partner and every person to be included... Since the tester repeats people.

Recommended Answers

All 4 Replies

And your question? One note though, I would use "names.length" rather than "18" in your random number generation. I would also make "r" an instance variable rather than creating a new one with every call to "getPartner".

my question is how do I not replicate any names when partners are picked?

Use a while loop and continue pulling a random second name until it does not match the first?

If you mean no replicates through the life of the program, then follow the advice in the first reply here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.