caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "François Pottier" <francois.pottier@inria.fr>
To: jean-marc.alliot@irit.fr, caml-list@inria.fr
Subject: Re: [Caml-list] Choosing a random element in a Map or a Set
Date: Thu, 18 Mar 2021 11:06:39 +0100	[thread overview]
Message-ID: <368aa12e-e819-4049-7ec5-4a3479f77bbb@inria.fr> (raw)
In-Reply-To: <3e61c49d-8b20-378b-0c24-75b8da14b4f7@alliot.org>

Le 18/03/2021 à 10:44, jean-marc.alliot@irit.fr a écrit :
> 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...

The tree is only approximately balanced, so this approach should be
OK if you are willing to accept some bias, but it is not perfectly
uniform.

If it is important to obtain a uniform distribution, then I would
suggest: 1- picking a random index in the interval [0, n); and 2-
using a data structure that allows efficient indexing. Finger trees
come to mind (see the papers by Hinze and Paterson and by Sozeau),
but there are probably others.

-- 
François Pottier
francois.pottier@inria.fr
http://cambium.inria.fr/~fpottier/

  reply	other threads:[~2021-03-18 10:07 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 [this message]
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

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=368aa12e-e819-4049-7ec5-4a3479f77bbb@inria.fr \
    --to=francois.pottier@inria.fr \
    --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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).