From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by c5ff346549e7 (Postfix) with ESMTP id 77EF75D5 for ; Wed, 27 Nov 2019 21:52:32 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.69,251,1571695200"; d="scan'208,217";a="413845936" Received: from sympa.inria.fr ([193.51.193.213]) by mail2-relais-roc.national.inria.fr with ESMTP; 27 Nov 2019 22:52:31 +0100 Received: by sympa.inria.fr (Postfix, from userid 20132) id 7EF947F332; Wed, 27 Nov 2019 22:52:31 +0100 (CET) Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 8C80B7F332 for ; Wed, 27 Nov 2019 22:52:24 +0100 (CET) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=ceastlund@janestreet.com; spf=Pass smtp.mailfrom=ceastlund@janestreet.com; spf=None smtp.helo=postmaster@mxout5.mail.janestreet.com IronPort-PHdr: =?us-ascii?q?9a23=3AKG7l1hdMTm1/G+JoIsUt9GPmlGMj4u6mDksu8pMi?= =?us-ascii?q?zoh2WeGdxcSzZx7h7PlgxGXEQZ/co6odzbaP6Oa5BzFLuMvY+Fk5M7V0Hycfjs?= =?us-ascii?q?sXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aFRrwLxd6?= =?us-ascii?q?KfroEYDOkcu3y/qy+5rOaAlUmTaxe7x/IAi4oAnLq8UbgItvJqksxhbJv3dDZv?= =?us-ascii?q?hby35vKV+PhRj3+92+/IRk8yReuvIh89BPXKDndKkmTrJWESorPXkt6MLkqRfM?= =?us-ascii?q?Qw2P5mABUmoNiRpHHxLF7BDhUZjvtCbxq/dw1zObPc3ySrA0RCii4qJ2QxLmlC?= =?us-ascii?q?sLKzg0+3zMh8dukKxUvg6upx1nw47Vfo6VMuZ+frjAdt8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?q?S9OrGi0rQ98thdQJZhQkQonwvlX4xyOvRoQtufmTHpVtrPDe1n78PNpnxnvakq?= =?us-ascii?q?Imigt+G5YdBSidnqd6sjPrKcvJnkGezfb4cLQbwTKQsmKKzG7IvkheXRVsS6jI?= =?us-ascii?q?QTYUYU6E9dk=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0AkCgAg795dhxLp10BlFggBCxyCAIMKV?= =?us-ascii?q?TIqhCuBHI1lghGZM4F6CQEDAQwjDAEBhEACggAcBwEENgQNAhABAQQBAQECAQI?= =?us-ascii?q?DBAETAQEBCA0JCCmFPgyCOykBgm4BAQECARIRBBkBATcBDwsEBzcCAiISAQUBH?= =?us-ascii?q?AYTIoMAAYJXIA+mbIEDPYslfzOCfgEBBYYSgT8JEoElhRqGexqBQT+BR4JdPoJ?= =?us-ascii?q?LGQKCEIJjgl6NNol3lxCCN4Y5ZI45G4MzlmyXBJFxDyOBHC0NgWpNI1AxBoI1C?= =?us-ascii?q?UcRFIZIGoNZgT6BIYgSIzOOQwEB?= X-IPAS-Result: =?us-ascii?q?A0AkCgAg795dhxLp10BlFggBCxyCAIMKVTIqhCuBHI1lghG?= =?us-ascii?q?ZM4F6CQEDAQwjDAEBhEACggAcBwEENgQNAhABAQQBAQECAQIDBAETAQEBCA0JC?= =?us-ascii?q?CmFPgyCOykBgm4BAQECARIRBBkBATcBDwsEBzcCAiISAQUBHAYTIoMAAYJXIA+?= =?us-ascii?q?mbIEDPYslfzOCfgEBBYYSgT8JEoElhRqGexqBQT+BR4JdPoJLGQKCEIJjgl6NN?= =?us-ascii?q?ol3lxCCN4Y5ZI45G4MzlmyXBJFxDyOBHC0NgWpNI1AxBoI1CUcRFIZIGoNZgT6?= =?us-ascii?q?BIYgSIzOOQwEB?= X-IronPort-AV: E=Sophos;i="5.69,251,1571695200"; d="scan'208,217";a="413845904" X-MGA-submission: =?us-ascii?q?MDGXiO8dJ+pB1yn8Wd2Xz+MnbmMSr0QyqDl61A?= =?us-ascii?q?onbyC0M/9tBv7rmT8ujPE04K7vkrNBGb0CDu+4w+2OgR3TwHZjV3EfP9?= =?us-ascii?q?aic/KfKM3fSlDRcgk+1xhGX0uh1r2rZwFn1yMH3SEvX3lSjEBxohmdXc?= =?us-ascii?q?9PtQLUt9XjRS2zUa4XoJeG9Q=3D=3D?= Received: from mxout5.mail.janestreet.com ([64.215.233.18]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 27 Nov 2019 22:52:23 +0100 X-JS-Received: from [30.40.81.8] (helo=tot-qpr-mailcore1) by mxout5.mail.janestreet.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) (envelope-from ) id 1ia5EU-0002SC-GS for caml-list@inria.fr; Wed, 27 Nov 2019 16:52:22 -0500 X-JS-Flow: external X-JS-Received: by tot-qpr-mailcore1 with ocaml/mailcore/main_production (148e96e5810f) (envelope-from ) id Bd3vAW-ELbi4A-M3; 2019-11-27 16:52:22.431154-05:00 X-JS-Scanner-attachment: No attachments X-JS-Scanner-esets: Not scanned (internal mail) Received: from mail-yb1-f199.google.com ([209.85.219.199]) by mxgoog2.mail.janestreet.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.2) id 1ia5EU-0001w1-Bm for caml-list@inria.fr; Wed, 27 Nov 2019 16:52:22 -0500 Received: by mail-yb1-f199.google.com with SMTP id j66so16534108ybg.8 for ; Wed, 27 Nov 2019 13:52:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0eTTUCmbU6jN5iiKfyGdruxzkvPaf4AoP9KFIu4rMQM=; b=OiDDUUYbnGkocF2efNJFqAqyUeBHhoRi7ltfGlW2T72K6lKTitIB+ibIjzxojdQesj qkpBZgoDfuEyuOJB6A/m+x1lFfRbIsmmNa8/9Dv532Ay2FWYox6bpnZYr0yH0l4Dxs0W z3NvXm5BN7Vvuv08HwTfhCx/Ls6OTWTft7i20= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0eTTUCmbU6jN5iiKfyGdruxzkvPaf4AoP9KFIu4rMQM=; b=clF+GAXNGUpHyNF5BjtKtuzzgSJrW8bqRd+DQfSUIZvIsGL89XAZ49kAbjA9JgW10Q Cd/i689iJQw1Zx5lD+qxd3tETX1KOMsR49dm/OQCJbETOlkoSlNqkgF1VH61CXtOqxEo v0Ucmz1hnLjK6xdLroA/ZMTwKYYxMSB/pdWR4kApdhlSjTBFXcHeilZN5icR+bECkayj XFSTeH4u2izXh2EHFWvHThAGchdtTs9zmGLBWYz+lbxFU4uNcV301WAAz9WFzpTXBL3M rAIbEmUhxd0huo4PeP7wjyFzShD/+GSJca7ETAFVKYEIZcyMDT0/W7aaZ5vBw5gYJgtN q8Yw== X-Gm-Message-State: APjAAAVrpQGdxlxbrjoGW1nX1tQICFSzC6V865i7DJFzAuKi1aM4uYAE 6ib9FK6LVpl+Ycpl0fdfSWEe3koiHja1G0AEaq+9VFf8Sd2zYRpxR/LpV11vXQS9bdKlgUbH7n5 syAeIkRH3UH0woX59RbKa5oEywA== X-Received: by 2002:a25:ba8a:: with SMTP id s10mr31980019ybg.346.1574891541961; Wed, 27 Nov 2019 13:52:21 -0800 (PST) X-Google-Smtp-Source: APXvYqzJbfT7br8ukISdASKW5FCBWgUyNhlCtsEoEatAkdP5X0ATv1qKoLFv2v+lmcAdo9s0PDJb5ISw71JLH9jjfCw= X-Received: by 2002:a25:ba8a:: with SMTP id s10mr31980002ybg.346.1574891541513; Wed, 27 Nov 2019 13:52:21 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Carl Eastlund Date: Wed, 27 Nov 2019 16:51:46 -0500 Message-ID: To: =?UTF-8?Q?Fran=C3=A7ois_Pottier?= Cc: caml users Content-Type: multipart/alternative; boundary="000000000000e4699a05985b0225" X-JS-Exim-Data-Received: 2019-11-27 16:52:22-0500 X-JS-Processed-by: mailcore Subject: Re: [Caml-list] zarith: how to pick a random integer? Reply-To: Carl Eastlund X-Loop: caml-list@inria.fr X-Sequence: 17887 Errors-to: caml-list-owner@inria.fr Precedence: list Precedence: bulk Sender: caml-list-request@inria.fr X-no-archive: yes List-Id: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --000000000000e4699a05985b0225 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable See the Make_random functor in the janestreet Bigint library, which is built on top of zarith. (It's a functor to produce random distributions based on both Random.State.t and Base_quickcheck.Generator.t; the functor itself is not exposed.) https://github.com/janestreet/bignum/blob/master/bigint/src/bigint.ml In short, we generate 30-bit chunks of randomness until we have at least enough bits for our range. We combine those into a number. Usually, we just modulo that by the range and return it. But to preserve fairness, we first have to check if the number is in the last fraction-of-range part of the N bits, and if so retry from scratch. The odds of retry are always less than 50%, so retrying is never too bad. This is the same trick that Random.int does, but with an unbounded number of bits instead of a fixed number of bits. On Wed, Nov 27, 2019 at 4:31 PM Fran=C3=A7ois Pottier wrote: > > Hello, > > I am using zarith and would like to pick a random integer > comprised between 0 and some bound. I would like a function > Z.random of type Z.t -> Z.t, but this function seems to be > missing, and I am not sure how to program it in an efficient > and correct way. Any suggestions would be welcome. Thanks! > > -- > Fran=C3=A7ois Pottier > francois.pottier@inria.fr > http://gallium.inria.fr/~fpottier/ > --=20 Carl Eastlund --000000000000e4699a05985b0225 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
See the Make_random functor in the janestreet=C2=A0Bigint = library, which is built on top of zarith. (It's a functor to produce ra= ndom distributions based on both Random.State.t and Base_quickcheck.Generat= or.t; the functor itself is not exposed.)


In short, we generate 30-bit chunks of = randomness until we have at least enough bits for our range. We combine tho= se into a number. Usually, we just modulo that by the range and return it. = But to preserve fairness, we first have to check if the number is in the la= st fraction-of-range part of the N bits, and if so retry from scratch. The = odds of retry are always less than 50%, so retrying is never too bad.
=

This is the same trick that Random.int does, but with a= n unbounded number of bits instead of a fixed number of bits.
On Wed, = Nov 27, 2019 at 4:31 PM Fran=C3=A7ois Pottier <francois.pottier@inria.fr> wro= te:

Hello,

I am using zarith and would like to pick a random integer
comprised between 0 and some bound. I would like a function
Z.random of type Z.t -> Z.t, but this function seems to be
missing, and I am not sure how to program it in an efficient
and correct way. Any suggestions would be welcome. Thanks!

--
Fran=C3=A7ois Pottier
francois.pot= tier@inria.fr
http://gallium.inria.fr/~fpottier/


--
Carl Eastlund
--000000000000e4699a05985b0225--