2

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;  
}
6
  • 1
    You don't need to have a loop there. Just select the random index and return the corresponding string. Commented Oct 5, 2012 at 8:58
  • The steps: select a number randomly that is less than array length, the return the array element? Can you translate this to code? Commented Oct 5, 2012 at 9:00
  • 1
    i think the main problem here is not how to get a random. since OP mentioned Each name should be selected with equal probability. I think he needs a uniformed random function. I don't think Random in java core library is uniformed. Commented Oct 5, 2012 at 9:02
  • @Kent int java.util.Random.nextInt(int n): Returns a new pseudo-random integer value which is uniformly distributed between 0 (inclusively) and n (exclusively) Commented Oct 5, 2012 at 9:07
  • @pankar checked the doc again. Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive).. u r right. thx. I think I was abit mixing up with real-random... Commented Oct 5, 2012 at 9:13

3 Answers 3

5

You can simply generate a random number up to the array size, and get the value at that index.

public static String selectRandom(String[] names) {
    if (name != null && names.length > 0) {
        Random r = new Random();
        return names[r.nextInt(names.length)];
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

4 Comments

you should add null check also otherwise if (names.length > 0) it may populate nullpointer exception.
@Vulcan Can you please tell me how can I choose a random string value from an array and also I want some other string every time. I mean if I don't want to repeat the same string value, what's modification I need in this code .
@Kunu Create another array of Strings (or booleans) and keep track of which Strings you've randomly picked already.
@Vulcan I was also thinking about that, but thought it would be wise to ask others first so if there any direct method exist in Random function I could do it easily. Thanks for your reply.
4
public static String selectRandom(String[] names)    
{    
      Random r = new Random();
      int ranNum= r.nextInt(names.length);
      return names[ranNum];

}

You don't need most of the codes inside your method. Maybe you should try something like this?

2 Comments

it may generate NullPointerException too.
@Russell Gutierrez Can you please tell me how can I choose a random string value from an array and also I want some other string every time. I mean if I don't want to repeat the same string value, what's modification I need in this code .
0

Randomly choose an index and return the corresponding String in names. There is a Random class to do get random numbers in java. Also check nextInt method.

public static String selectRandom(String[] names)    
{    
   Random rand = new Random();
   int index = rand.nextInt(names.length);
   return names[index];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.