alanbuchanan
alanbuchanan

Reputation: 4173

Calling a random value element from a List

I would like to be able to call a random string from a list or array of strings. The reason I want to do this is to be able to make up a story, for example, or a piece of text that is different every time. So far, the way I have done it is:

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

class Story {

    Random myRandom = new Random();
    int index = myRandom.nextInt(10);

    public static void main(String[] args) {
        List<String> adjective = new ArrayList<String>();
        adjective.add("efficacious");
        adjective.add("propitious");
        adjective.add("trenchant");
        adjective.add("arcadian");
        adjective.add("effulgent");

        Story obj = new Story();

        System.out.println("This is going to be one " + obj.getRandAdjective(adjective) + " story.");
    }

    public String getRandAdjective(List<String> adjective) {
        int index = myRandom.nextInt(adjective.size());
        return adjective.get(index);
    }
}
  1. What is a more effective way of structuring this, bearing in mind further lists will be added?
  2. Is it possible to structure the code to the extent that a shorter version of obj.getRandAdjective(adjective) is written within the story (mainly for readability)?

Upvotes: 0

Views: 171

Answers (3)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

Here's one way to generalize what you're doing to the various parts of speech. I put the classes all together to make it easier to paste. You should make these separate public classes.

package com.ggl.testing;

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

public enum PartsOfSpeech {
    ADJECTIVE, ADVERB, ARTICLE, CONJUNCTION, INTERJECTION, NOUN,              
    PREPOSITION, PRONOUN, VERB
}

class Word {

    private final PartsOfSpeech partOfSpeech;

    private final String word;

    public Word(PartsOfSpeech partOfSpeech, String word) {
        this.partOfSpeech = partOfSpeech;
        this.word = word;
    }

    public PartsOfSpeech getPartOfSpeech() {
        return partOfSpeech;
    }

    public String getWord() {
        return word;
    }

}

class Sentence {

    private List<Word> words;

    private Random random;

    public Sentence() {
        this.words = createWordList();
        this.random = new Random();
    }

    private List<Word> createWordList() {
        List<Word> words = new ArrayList<Word>();
        words.add(new Word(PartsOfSpeech.VERB, "run"));
        // add the rest of the words here

        return words;
    }

    public Word getWord(PartsOfSpeech partsOfSpeech) {
        List<Word> subList = getSubList(partsOfSpeech);
        int index = random.nextInt(subList.size());
        return subList.get(index);
    }

    private List<Word> getSubList(PartsOfSpeech partsOfSpeech) {
        List<Word> subList = new ArrayList<Word>();
        for (Word word : words) {
            if (word.getPartOfSpeech() == partsOfSpeech) {
                subList.add(word);
            }
        }

        return subList;
    }

}

Upvotes: 1

Abdul Ahmad
Abdul Ahmad

Reputation: 10021

I'd suggest adding all the lists as static variables inside this class instead of having a bunch of lists inside your main method.

And if you already know what words you want in the lists declare them with those words, instead of doing adds. You can add a method addWordToList(List<String> list) or something similar to add words at run time

For example:

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

class Story {

Random myRandom = new Random();
int index = myRandom.nextInt(10);
static List<String> adjective = new ArrayList<String> {
    "efficacious","propitious","trenchant","arcadian","effulgent"};
static List<String> noun = new ArrayList<String> {
    "efficacious","propitious","trenchant","arcadian","effulgent"};
static List<String> verb = new ArrayList<String> {
    "efficacious","propitious","trenchant","arcadian","effulgent"};

//etc... change the words in the lists

public static void main(String[] args) {
    Story obj = new Story();

    System.out.println("This is going to be one " + obj.getRandWord(adjective) + " story.");
}

public String getRandWord(List<String> words) {
    int index = myRandom.nextInt(adjective.size());
    return words.get(index);
}
}

if this is going to become a larger program, you're probably best off making a static class that just has lists of words and you can access them through Class.List

this will keep code cleaner and more organized

Upvotes: 1

qcGold
qcGold

Reputation: 200

You can use the Math.Random to generate a random number from 0.0 to 1.0

If you tweak it a bit and play around with it you can use it to do what you need. Like this example:

    List<String> adjective = new ArrayList<String>();
    adjective.add("efficacious");
    adjective.add("propitious");
    adjective.add("trenchant");
    adjective.add("arcadian");
    adjective.add("effulgent");

    System.out.println(adjective.get((int) Math.floor(Math.random() * adjective.size())));

Outputs random elements of your List

Upvotes: 0

Related Questions