From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 40EA0BBAF for ; Wed, 18 Nov 2009 11:59:09 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ag0BAENiA0tKfU4YkGdsb2JhbACERpZ9PwEBAQEJCQwHEwOnU4FhhluIawEDAwWBK4I3VASBb4EDh3Q X-IronPort-AV: E=Sophos;i="4.44,764,1249250400"; d="scan'208";a="40330230" Received: from ey-out-2122.google.com ([74.125.78.24]) by mail1-smtp-roc.national.inria.fr with ESMTP; 18 Nov 2009 11:59:08 +0100 Received: by ey-out-2122.google.com with SMTP id d26so99639eyd.7 for ; Wed, 18 Nov 2009 02:59:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:date :x-google-sender-auth:message-id:subject:from:to:content-type; bh=Wp061+cPwhpXO9KUXmcmy/07XesNYXOKG+wjU6HHUMY=; b=bqhIZOme2rGk3QU4tZB9N0yGLXrcB6r0RZ249ga65z+THe2MPNmHUR6uDDxHg4fT3A L5N0aimQALlu49GyQBjsilTLj0YwqS14zaZ54PdJeueuOTt6NPt+nO+rIY3gBm5ajlrP pxLgpTaShK24oUdrJVvzQOlJmW0d+leZYf1XA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:date:x-google-sender-auth:message-id:subject :from:to:content-type; b=sRPKTEjC5WG12iz8YH02KOKDI0QugxFJtx1xz2K61zo6E0IVaR2oN84ai7flYP/3T8 dK/6yTcGIL7MI5htAXmp/J+eopVgmhJ/zVKlsQDcLhcShKxt55s9uUNvKt/g0+8VxU3y q2NMT8Cz008+/Hd1ZtqLzb+8HQBXGa1DoFexw= MIME-Version: 1.0 Sender: daniel.c.buenzli@gmail.com Received: by 10.213.23.146 with SMTP id r18mr193080ebb.46.1258541948730; Wed, 18 Nov 2009 02:59:08 -0800 (PST) Date: Wed, 18 Nov 2009 18:59:08 +0800 X-Google-Sender-Auth: 2ff5573edb33d357 Message-ID: <91a3da520911180259s585b5ef5j311bd0448ed3c02f@mail.gmail.com> Subject: Random questions From: =?UTF-8?Q?Daniel_B=C3=BCnzli?= To: caml-list@inria.fr Content-Type: text/plain; charset=UTF-8 X-Spam: no; 0.00; buenzli:01 rec:01 functions:01 functions:01 int:01 int:01 arbitrary:02 arbitrary:02 bounds:02 bounds:02 lsl:03 arg:03 arg:03 preserve:03 module:03 I know little about PRGN and unfortunately in a lot of cases the functions in the Random module don't provide me the right interface. Could anybody tell me if the following functions preserve the quality of the underlying PRGN and/or if there's a better way to achieve that : 1) Generate an arbitrary int let rint () = Random.bits () lor ((Random.bits () land 1) lsl 30) 2) Generate an arbitrary int in [0;max] (bounds included) let random_uint ?(max = max_int) = if max < 0 then invalid_arg "negative max" else if max = max_int then Random.bits else let bound = max + 1 in fun () -> Random.int bound 3) Generate an arbitrary int in [-max;max] (bounds included) let random_int ?(max = max_int) = if max < 0 then invalid_arg "negative max" else if max = max_int then let rec aux () = let v = rint () in if v = min_int then aux () else v in aux else let bound = (2 * max) + 1 in if 0 < bound && bound < max_int then fun () -> -max + Random.int bound else let bound = Int32.add (Int32.mul 2l (Int32.of_int max)) 1l in let min = Int32.of_int (-max) in fun () -> Int32.to_int (Int32.add min (Random.int32 bound)) 5) Generate an arbitrary float in [0;max] (bounds included) let after_one = 1. +. epsilon_float let random_ufloat ?(max = max_float) = if max < 0. then invalid_arg "negative max" else fun () -> (Random.float after_one) *. max 6) Generate an arbitrary float in [-max;max] (bounds included) let after_one = 1. +. epsilon_float let random_float ?(max = max_float) = if max < 0. then invalid_arg "negative max" else fun () -> (-1. +. (Random.float after_one) *. 2.) *. max Thanks for your answers, Daniel