How to select a random element in std::set in less than O(n) time?

后端 未结 5 907
[愿得一人]
[愿得一人] 2021-01-01 20:30

This question with an added constraint.

I\'m willing to allow not-uniform selection as long as it\'s not to lop sided.

Given that \"sets are typically imple

5条回答
  •  有刺的猬
    2021-01-01 21:06

    Introduce array with size equal to set. Make array elements hold addresses of every element in set. Generate random integer R bounded by array/set size, pick address in array's element indexed by R and dereference it to obtain set's element.

提交回复
热议问题