question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get random element from set

See original GitHub issue

What’s the most efficient way to implement something like RoaringBitmap.random()?

For example, RoaringBitmap.first() and RoaringBitmap.last() return the smallest and largest elements… but how would you obtain a random element?

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
blacellecommented, Sep 13, 2017

What about

Random random = new Random();
bitmap.select(random.nextInt(this.getCardinality()));
1reaction
lemirecommented, Sep 20, 2017

If you have a random pool, it is not going to be efficient to represent it solely as ranges, like you did. I think that the reason the ranges are efficient, is because of the strong statistical bias you have introduced.

That is, the minute you get close to a half empty interval, if you picked the elements at random, a set of ranges won’t be close to optimal storage-wise.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Picking a random element from a set - java - Stack Overflow
toArray() on the supplied set. ToArray (in most if not all standard collection implementations) iterates over the entire collection, filling an array as...
Read more >
How to Get Random Elements from Java HashSet?
Firstly convert HashSet into an array and then access the random element from it. · Then we will create an object of Random...
Read more >
Select Random Element from Set in Python - CodeSpeedy
Select Random Element from Set in Python · random.choice(). This function retrieves a single element from the set. · random.choices() import random movie_list...
Read more >
Java - get random element from set - Dirask
Iterate over the set and generate random number within size of the set. · Iterate over the elements of the set and keep...
Read more >
Get a random element from a Set in Python | bobbyhadz
To get a random element from a `set`, use the `list()` class to convert the `set` to a list. Use the `random.choice()` method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found