How to generate a given number of random objects with a given number of attributes?

My question is: how to create a random task with a given number (4-8) of tests?

The task is created from tests (see code below).

Tests are attributes of a class Challengeconstructed from an enumeration, and EnumMap.

Therefore, when I get a random number in the main (e.g. r.nextInt(4) + 4;), I will create this number of tests to call as follows

Weapons = 25;
Hacking = 32;
Vehicle = 34;
Speed = 56;

So, for 1 Challenge, I do not need all the tests, only some of them. But I still need the opportunity to choose from them randomly (4-8).

How can I generate these random objects Challengewith a given number of attributes (and their values)? So basically some of them, from ALL of them.

? ?

:

import java.util.*;

public class Challenge {
    Random r = new Random();

    // DRIVE, VEHICLE, ACCURACY, WEAPONS, REFLEX, STRATEGY, CHARISMA, HACKING,
    // SPEED, STEALTH;
    public static enum Trial {
        DRIVE, VEHICLE, ACCURACY, WEAPONS, REFLEX, STRATEGY, CHARISMA, HACKING, SPEED, STEALTH;
    }

    Map<Trial, Integer> challenge = new EnumMap<Trial, Integer>(Trial.class);

    public Challenge() {
        challenge.put(Trial.DRIVE, r.nextInt(100) + 25);
        challenge.put(Trial.VEHICLE, r.nextInt(100) + 25);
        challenge.put(Trial.ACCURACY, r.nextInt(100) + 25);
        challenge.put(Trial.WEAPONS, r.nextInt(100) + 25);
        challenge.put(Trial.REFLEX, r.nextInt(100) + 25);
        challenge.put(Trial.STRATEGY, r.nextInt(100) + 25);
        challenge.put(Trial.CHARISMA, r.nextInt(100) + 25);
        challenge.put(Trial.HACKING, r.nextInt(100) + 25);
        challenge.put(Trial.SPEED, r.nextInt(100) + 25);
        challenge.put(Trial.STEALTH, r.nextInt(100) + 25);
    }

    List<Trial> keys = new ArrayList<Trial>(challenge.keySet());
    Trial randomKey = keys.get(r.nextInt(keys.size()));
    Integer value = challenge.get(randomKey);
}
+4
3

, t , int t = random.nextInt(5) + 4, 4 <= t <= 8 ( , nextInt ). random.nextInt(100) + 25 ( 25 124), .

. t , t. .

Map<Trial, Integer> challenge = new EnumMap<Trial, Integer>(Trial.class);
int t = 4 + random.nextInt(5);
// collect all Trial values in a list
List<Trial> allTrials = Arrays.asList(Trial.values());
// shuffle that list
Collections.shuffle(allTrials);
// select the number of trials we want, add them to challenge
for (int i = 0; i < t; ++i) {
  challenge.put(allTrials.get(i), random.nextInt(100) + 25);
}

// example output: {DRIVE=121, WEAPONS=119, STRATEGY=59, HACKING=78, STEALTH=72}
System.out.println(challenge);
+2

, : ?

; values(), ; . , ; .

Trial allTrials[] = Trial.values();
Random random = new Random();

Trial pickRandomTrial() {
  return allTrials[random.nextInt(allTrials.length)];
}
+2

But I still need the ability to select from them randomly (4-8).

you can use

    Random r = new Random();
    int min = 4;
    int max = 8;

    System.out.println(r.nextInt((max - min) + 1) + min);
0
source

Source: https://habr.com/ru/post/1674385/


All Articles