samedi 29 mai 2021

Why, when generating random numbers, the number of which corresponds to the maximum size of the number, unique numbers are always 0.63 of the size?

package com.company; import java.util.*;

public class Main {

public static void main(String[] args) {
    ArrayList<Integer> integers = new ArrayList<>();
    for (int i = 0; i < 20000000; i++) {
        integers.add((int) (Math.random()*20000000));
    }
    HashSet<Integer> unique = new HashSet<>(integers);

    System.out.println("Number of items in a list of numbers: " + integers.size());
    System.out.println("Number of unique elements: " + unique.size());
}
}



Aucun commentaire:

Enregistrer un commentaire