caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Francois Berenger <mlists@ligand.eu>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Choosing a random element in a Map or a Set
Date: Fri, 19 Mar 2021 10:32:30 +0900
Message-ID: <c82b48ad959246e387805181166585e1@ligand.eu> (raw)
In-Reply-To: <aa6c6dc8-a1c7-f0a5-0433-c6c731451fc3@recherche.enac.fr>

On 18/03/2021 20:10, Nicolas Barnier wrote:
> Hi Jean-Marc,
> 
> Le 18/03/2021 à 10:44, jean-marc.alliot@irit.fr a écrit :
>> 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?
> 
> If using a Map or a Set is not compulsory, you can design a data 
> structure
> supporting insert, delete, search and random element in constant 
> amortized
> time with an extensible array and a hash-table as explained here:

Just FTR, a skip list would allow all the set operations, plus an 
efficient random
access operation and element rank.

Pugh, William. (1990). A skip list cookbook.

I am not aware of an OCaml implementation with all the interesting
operations though.

> https://www.geeksforgeeks.org/design-a-data-structure-that-supports-insert-delete-search-and-getrandom-in-constant-time/
> 
> I don't know if there are any good implementation of (Python-like)
> extensible arrays in a widespread OCaml library, but François Pottier
> published one as part of another superb data structure (union-find):
> 
> https://gitlab.inria.fr/fpottier/unionfind/-/blob/master/src/StoreVector.ml
> 
> You may want to adjust the ratio (2 here) by which the array is resized 
> when
> necessary.
> 
> Hope this helps,
> 
> -- Nicolas

  parent reply	other threads:[~2021-03-19  1:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-18  9:44 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 [this message]
2021-03-18 12:34 ` Ivan Gotovchits
2021-03-18 12:48 ` Ivan Gotovchits

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c82b48ad959246e387805181166585e1@ligand.eu \
    --to=mlists@ligand.eu \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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