caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Choosing a random element in a Map or a Set
@ 2021-03-18  9:44 jean-marc.alliot
  2021-03-18 10:06 ` François Pottier
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: jean-marc.alliot @ 2021-03-18  9:44 UTC (permalink / raw)
  To: caml-list

Dear list,

I am trying to take a random element from a Map or a Set.

Currently, I generate one random int between 1 et Card(map), and I iter
until I reach this element. The solution is in O(n) which is not great...

I was about to code the following solution: take the current Map code
and add a function which follows the balanced binary tree from the root
and takes randomly a left or right turn at each node until we reach a
leaf (we only need to generate one random int and use its binary
representation to choose the left or right direction at each node). This
should be in O(log(n))

Before I start coding like an idiot:
1) Is there another, more intelligent, solution?
2) Is my solution biased? I think it is not, as long as the tree is
properly balanced but...

Thanks

Jean-Marc



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-03-19  1:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-18  9:44 [Caml-list] Choosing a random element in a Map or a Set jean-marc.alliot
2021-03-18 10:06 ` François Pottier
2021-03-18 14:20   ` Simon Cruanes
2021-03-18 17:06     ` jean-marc.alliot
2021-03-18 11:10 ` Nicolas Barnier
2021-03-18 11:32   ` François Pottier
2021-03-19  1:32   ` Francois Berenger
2021-03-18 12:34 ` Ivan Gotovchits
2021-03-18 12:48 ` Ivan Gotovchits

caml-list - the Caml user's mailing list

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/caml-list
	git clone --mirror https://inbox.ocaml.org/caml-list

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 caml-list caml-list/ http://inbox.vuxu.org/caml-list \
		caml-list@inria.fr
	public-inbox-index caml-list

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.caml-list


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git