-1

I've been creating a random element generator of my first text adventure game, to be more specific, this:

    Random Generation = new Random();
    List<Integer> interact = new ArrayList<Integer>();
    for (int generator = Generation.nextInt(5) + 3; generator > 0; generator--) {
        interact.add(Generation.nextInt(10));
    };
    System.out.println(interact);

Here, generator defines the amount of elements, and interact its a list that contains random numbers which represent different interactive objects, the thing is, i need elements don't repeat.

I was thinking in a int a = Generation.nextInt(3); if (a != interact[]) {interact.add(a)} else {generator++}; but I don't know how to set an [any] in comparing arrays.

TheBigUan
  • 21
  • 6
  • 4
    "*i've seen some previous questions, but i don't think they apply to my case*" => so you've seen the suggestion to put the numbers in an array and shuffle? Why doesn't it work for you? – assylias Sep 21 '16 at 16:24
  • Actually i didn't understand you perfectly ,do you want to generate random unique key ? – Basil Battikhi Sep 21 '16 at 16:26
  • @BasilBattikhi I think yeah, i need the generator not repeat elements, but always maintaining that numbers of elements of generator var, i edited the question, i maked an error in interact range, i made it more big. – TheBigUan Sep 21 '16 at 16:30
  • What you want to achieve is unclear –  Sep 21 '16 at 16:34
  • @assylias actually i dont think that one appeared when i searched, but just to make clear, i need that generator creates the exact amount of elements than generator, so maybe it can avoid be repeateable, but if it's something like a set, the `generator--` will still activate, and game will have 1 less element than what it should. – TheBigUan Sep 21 '16 at 16:42
  • @RC i need that generator creates the exact amount of elements than generator, AND avoid be repeatable, so if it's something like a set, the generator-- will still activate, and game will have 1 less element than what it should. – TheBigUan Sep 21 '16 at 16:45

2 Answers2

0

IIRC your requirements are:

  • Game must have an "element list" of size 3 <= numberOfItems <= 8. Where numberOfItems must be randomized each time.
  • The "element list" must contain exactly numberOfItems random integers between 0 and 10.

How about this?

    Random generator = new Random();
    Set<Integer> interact = new HashSet<Integer>();
    int numberOfItems = generator.nextInt(5) + 3;
    while (interact.size() < numberOfItems) {
        interact.add(generator.nextInt(10));
    }

It will keep adding distinct elements to interact until it reaches the designated capacity numberOfItems.

walen
  • 7,103
  • 2
  • 37
  • 58
-1

Modify your for loop this way:

for (int generator = Generation.nextInt(5) + 3; generator > 0; generator--) {
        int randomedNumber = 0;
        do {
          randomedNumber = Generation.nextInt(10);
        } while(interact.contains(randomedNumber))
        interact.add(randomedNumber);
};

You random a number, if it's in a list - you random again, if it's not - you add it to the list and move on to the next, pretty straightforward.

Shadov
  • 5,421
  • 2
  • 19
  • 38