Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get 100 random elements from HashSet in Java?

I have a HashSet in which I have 10000 elements. I want to extract random 100 elements from that HashSet. So I thought I can use shuffle on the set but it doesn't work.

Set<String> users = new HashSet<String>();

// for randomness, but this doesn't work
Collections.shuffle(users, new Random(System.nanoTime()));  

// and use for loop to get 100 elements

I cannot use shuffle now, is there any other best way to get 100 random elements from HashSet in Java?

like image 831
user1950349 Avatar asked Dec 14 '22 15:12

user1950349


2 Answers

Without building a new list, you can implement the following algorithm:

n = 100
d = 10000  # length(users)
for user in users:
    generate a random number p between 0 and 1
    if p <= n / d:
       select user
       n -= 1
    d -= 1

As you iterate through the list, you decrease the probability of future elements from being chosen by decreasing n, but at the the same time increase the probability by decreasing d. Initially, you would have a 100/10000 chance of choosing the first element. If you decide to take that element, you would have a 99/9999 chance of choosing the second element; if you don't take the first one, you'll have a slightly better 100/9999 chance of picking the second element. The math works out so that in the end, every element has a 100/10000 chance of being selected for the output.

like image 114
chepner Avatar answered Dec 17 '22 04:12

chepner


Shuffling the collection implies that there is some defined order of elements within, so elements can be reordered. HashSet is not an ordered collection as there is no order of elements inside (or rather details of the ordering are not exposed to the user). Therefore implementation wise it's does not makes much sense to shuffle HashSet.

What you can do is add all elements from your set to the ArrayList, shuffle it and get your results.

List<String> usersList = new ArrayList<String>(users);
Collections.shuffle(usersList);
// get 100 elements out of the list
like image 32
Dmitry Zaytsev Avatar answered Dec 17 '22 04:12

Dmitry Zaytsev