I am very new to Java, so this could seem too easy for most people....Is this completely wrong?
My question is how to write a method selectRandom(String[] names),
which returns a randomly selected name from the given array.
Each name should be selected with equal probability.
public static String selectRandom(String[] names)
{
String num = names[0];
int[]newArray = new int[names.length];
for(int i =0; i<names.length;i++)
{
Random r = new Random();
int ranNum= r.nextInt(names.length)+1;
num = names[ranNum];
}
return num;
}
int java.util.Random.nextInt(int n): Returns a new pseudo-random integer value which is uniformly distributed between 0 (inclusively) and n (exclusively)