简体   繁体   中英

Printing a random string from my ArrayList to the console

Here's my code:

public static void main(String[] args) {

    ArrayList<String> eMinor = new ArrayList<String>();
    eMinor.add("E");
    eMinor.add("F#");
    eMinor.add("G");
    eMinor.add("A");
    eMinor.add("B");
    eMinor.add("C");
    eMinor.add("D");

}

What do I need to add in order to generate a random string from the list and print it in the console?

You can randomly select a value from the list with something like:

int index = new java.util.Random().nextInt(eMinor.size());
String value = eMinor.get(index);

and then print it to the console with:

System.out.println(value);

e minor is one of my favorite keys, so I'll answer. You need a random number generator:

Random ran = new Random(); 
int x = ran.nextInt(eMinor.size() - 1)
System.out.println(eMinor.get(x));

You could generate multiple notes with a loop:

Random ran = new Random(); 
for (int i = 0, i < 10; i++) {
    int x = ran.nextInt(7)
    System.out.println(eMinor.get(x));
}
public static void main(String[] args) {

    ArrayList<String> eMinor = new ArrayList<String>();
    eMinor.add("E");
    eMinor.add("F#");
    eMinor.add("G");
    eMinor.add("A");
    eMinor.add("B");
    eMinor.add("C");
    eMinor.add("D");

    Random rand = new Random()
    int random = rand.nextInt(eMinor.size());
    String note = eMinor.get(random);
    System.out.println(note);
}

You could do something like this:

int min = 3;
int range = 3;
Random random = new Random(); 
int length = min + random.nextInt(range);
String randomString = "";

for (int i = 0; i < length; ++i)
{
    randomString += eMinor.get(random.nextInt(eMinor.size() - 1));
}

System.out.println(randomString);

You can use a simple model, but to complex application is necessary more attention with Random class;

enter code here
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {

 public static void main(String[] args) {

    ArrayList<String> eMinor = new ArrayList<String>();
    eMinor.add("E");
    eMinor.add("F#");
    eMinor.add("G");
    eMinor.add("A");
    eMinor.add("B");
    eMinor.add("C");
    eMinor.add("D");

    for (int f = 0; f < 3; f++) {
        System.out.printf("------------ list %s ------------\n",f);
        for (String value : generateList(eMinor)) {
            System.out.println(value);
        }
    }
 }

 private static List<String> generateList(ArrayList<String> eMinor) {

    List<String> tempList = new ArrayList<>();
    Random random = new Random();

    while (tempList.size() != eMinor.size()) {
        String value = eMinor.get(random.nextInt(eMinor.size()));
        if (!tempList.contains(value)) {
            tempList.add(value);
        }
    }

    return tempList;

 }
}

You may use the below complete example:


/* * Created by Mohammed.Kharma on 2/9/2016. */

import java.util.Random;

import java.util.ArrayList;

public class Scrambler {

public static int[] Scramble(final int key, final int elementsCount) throws NegativeArraySizeException {
    if (elementsCount < 0) {
        NegativeArraySizeException ex = new NegativeArraySizeException("scrambler elementCount");
        throw ex;
    }

    Random rand = new Random(key);
    int[] order = new int[elementsCount];
    for (int i = 0; i < elementsCount; i++) {
        order[i] = i;
    }
    int count = elementsCount;
    while (--count > 0) {

        int nextRandom = rand.nextInt(count + 1);  // 0 <= k <= count (!)
        int temp = order[count];
        order[count] = order[nextRandom];
        order[nextRandom] = temp;

    }
    return order;
}

public static void main(String[] args) {
    ArrayList<String> eMinor = new ArrayList<String>();
    eMinor.add("E");
    eMinor.add("F#");
    eMinor.add("G");
    eMinor.add("A");
    eMinor.add("B");
    eMinor.add("C");
    eMinor.add("D");

    for (int numOFRandoStrings = 0; numOFRandoStrings < 10; numOFRandoStrings++) {
        int[] randomList = Scramble(new Long(System.currentTimeMillis()).intValue() * numOFRandoStrings, 7); //numOFRandoStrings or any seed,7 means give me 7 random numbers from zero to 6 
        StringBuffer randomString = new StringBuffer();
        for (int ind = 0; ind < randomList.length; ind++) {
            randomString.append(eMinor.get(randomList[ind] ));
        }
        System.out.println("New Random String: " + randomString);
    }

}

}

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