简体   繁体   中英

Create a Random String

How can I create a random string from these 4 strings? I need to take only 2 strings from there. I tried to do something like that, but it's not working.

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

You can do it this way also

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());

This shall give you comination of two Strings picked randomly

        //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);

This is working alghoritm with some properties to choose on, without duplicities and with any size of input.

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);
    }
}

Sample output is :

[Message 2, LOL]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM