vendredi 25 décembre 2020

Generating y random numbers and inserting them into a HashMap

I need to create a function that returns a random number from 1 to x and I have to generate y of them. Results must be in HashMap. At this moment the user can pass x and y values, code generates y random numbers from 1 to x but I totally don't know how to insert results in HashMap that is created.

EDIT: About HashMap, key = number from 1 to x, value = amount of generated numbers

private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
public static void main (String[] args) throws IOException {

    System.out.println("Insert x value: ");
    int x = Integer.parseInt(bufferedReader.readLine());

    System.out.println("Insert y value: ");
    int y = Integer.parseInt(bufferedReader.readLine());

    
    HashMap<Integer, Integer> map = new HashMap <Integer, Integer>();
    
Random random = new Random();
    for (int i = 1; i <= y; i++) {
        int value = 1 + random.nextInt(x);
        System.out.println(value);
    }


}



Aucun commentaire:

Enregistrer un commentaire