caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ivan Gotovchits <ivg@ieee.org>
To: jean-marc.alliot@irit.fr
Cc: caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Choosing a random element in a Map or a Set
Date: Thu, 18 Mar 2021 08:48:34 -0400
Message-ID: <CALdWJ+yx7bXCn+VTg-PcECnbj_TYMZbnyqpkx_fXAgyaappxxw@mail.gmail.com> (raw)
In-Reply-To: <3e61c49d-8b20-378b-0c24-75b8da14b4f7@alliot.org>

[-- Attachment #1: Type: text/plain, Size: 1014 bytes --]

On Thu, Mar 18, 2021 at 5:41 AM <jean-marc.alliot@irit.fr> wrote:

> 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
>

As a side note, in case if you decide to pursue this idea. You don't really
need to take the code as you can implement your solution using the public
interface.
For that, you need only these two public functions: `find_first` and
`split`. The `find_first` function will help us to extract the top element
(`let top t = fst@@find_first (fun _ -> true) t`),
and `let destruct t = split (top t) t` will return you the left and the
right subtrees, and the top element.  The `destruct` function will not
allocate or rebalance any trees and conceptually
will just return pointers to the corresponding subtrees. So it is O(1) both
in space and time.

[-- Attachment #2: Type: text/html, Size: 1420 bytes --]

      parent reply	other threads:[~2021-03-18 12:50 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
2021-03-18 12:34 ` Ivan Gotovchits
2021-03-18 12:48 ` Ivan Gotovchits [this message]

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=CALdWJ+yx7bXCn+VTg-PcECnbj_TYMZbnyqpkx_fXAgyaappxxw@mail.gmail.com \
    --to=ivg@ieee.org \
    --cc=caml-list@inria.fr \
    --cc=jean-marc.alliot@irit.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