创建随机字符串

时间:2014-09-09 14:41:15

标签: java string random

如何从这4个字符串创建随机字符串?我需要从那里只拿2个字符串。 我试图做那样的事情,但它不起作用。

String RandomMessages[] =
    {
           "Message 1 (Change this)",
           "Message 2",
           "LOL",
           "fdasfsd"
    };
public static void main(String[] args) {
      System.out.println(RandomMessage[]);
}

3 个答案:

答案 0 :(得分:2)

你也可以这样做

List<String> list = Arrays.asList(RandomMessages);
Collections.shuffle(list);
StringBuilder builder = new StringBuilder();
builder.append(list.get(0)+" , "+list.get(1));
System.out.println(builder.toString());

这会让你随机选择两个字符串

答案 1 :(得分:0)

        //Please note - you will need to import java.util.Random;

        String[] randomMessages = new String[]{
                   "Message 1 (Change this)",
                   "Message 2",
                   "LOL",
                   "fdasfsd"
            };

        Random randomGenerator = new Random();
        int i = randomGenerator.nextInt(randomMessages.length);

        String s = randomMessages[i];

        int oldi = i;

        Random randomGenerator2 = new Random();
        i = randomGenerator2.nextInt(randomMessages.length);

        //Loop until we have a different index.
        while (i = oldi) {
             i = randomGenerator2.nextInt(randomMessages.length);
        }

        s = s + randomMessages[i];

        System.out.println(s);

        //Alternative suggestion from Richard Le Mesurier - using Collections.shuffle
        List list = Arrays.asList(randomMessages);
        Collections.shuffle(list);
        String s2 = list.get(0).toString() + list.get(1).toString();

        System.out.println(s2);

答案 2 :(得分:-5)

这是一种工作算法,可以选择一些属性,没有重复项和任何大小的输入。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class JavaApplication191 {
    public static String randomMessages[]
            = {
                "Message 1 (Change this)",
                "Message 2",
                "LOL",
                "fdasfsd"
            };

    public static void main(String[] args) {
        int howManyWords = 2; //this is how many words you want in your output
        List<String> listOfWords = new ArrayList<>(); //here you create new list
        Random randomGenerator = new Random(); //this is randomGenerator, it helps you generate random numbers
        listOfWords.addAll(Arrays.asList(randomMessages)); //You can use listOfWords.add("LOL") to add new property, or you can add array like this
        List<String> selectedRandomMessages = new ArrayList<>(); //here I put output
        for (int i = 0; i < howManyWords; i++) {
            int randomNumber = randomGenerator.nextInt(listOfWords.size()); //I generate random number from 0 to size of list -1
            String randomItem = listOfWords.get(randomNumber); //here I select string by index I generated
            selectedRandomMessages.add(randomItem); //now I add this word into my output list
            listOfWords.remove(randomItem); //and I remove this item from input list, so I do not get any duplicities
        }        
        System.out.println(selectedRandomMessages);
    }
}

示例输出为:

[Message 2, LOL]