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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id DB6B8BBAF for ; Thu, 3 Dec 2009 15:48:38 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvUAAKteF0vRVdvTimdsb2JhbACEOJcYPwEBAQoJDAcRBas2gV2GIIhqAQIDBYEqgixXBIFngRiIJg X-IronPort-AV: E=Sophos;i="4.47,335,1257116400"; d="scan'208";a="39212499" Received: from mail-ew0-f211.google.com ([209.85.219.211]) by mail3-smtp-sop.national.inria.fr with ESMTP; 03 Dec 2009 15:48:38 +0100 Received: by ewy3 with SMTP id 3so1594807ewy.22 for ; Thu, 03 Dec 2009 06:48:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=3QlmWIBIl6ck5LILVYO0a+bdG3A6wNDPHnSjit7oFRo=; b=KmXAQ3RV/dPTu0rxOJ4ANTjsd1iKP3il8jcqsex84Mg3v+ABO40dA1vnsJCU7omJLW l5kqw6hIz+9oF3Z1HlJplj/Rns1kV/Aet81TpkxoJdUu9ssKuzXUAAq0JS+awqQzmYUa ReVxphAMNJkPwSiaF9BADuHFUOQMWRaHypjl4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=Q+8/6zHex9v5et8iro1vpPi7pDu4rnPm+3sFUtoRSuP4lHaUvCXsoWghbpxbkzjQjm bOghE3sOLkBX8KIdpHly7PEANVjNzgcBlZI0sTEofNMUtcvDje6d8F/mvXkjonLYEdUN HW12e4GgV0hRJqQijqFbQDg/3+F6CvHkGjSok= MIME-Version: 1.0 Sender: daniel.c.buenzli@gmail.com Received: by 10.213.109.148 with SMTP id j20mr9107514ebp.2.1259851717801; Thu, 03 Dec 2009 06:48:37 -0800 (PST) In-Reply-To: References: <91a3da520911180259s585b5ef5j311bd0448ed3c02f@mail.gmail.com> Date: Thu, 3 Dec 2009 22:48:37 +0800 X-Google-Sender-Auth: ea2dae14ca4bf95a Message-ID: <91a3da520912030648u62aac252g6faddf06892646d9@mail.gmail.com> Subject: Re: [Caml-list] Random questions From: =?UTF-8?Q?Daniel_B=C3=BCnzli?= To: AUGER Cc: caml-list@inria.fr Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; buenzli:01 bool:01 bool:01 semantics:01 exception:01 inline:01 caml-list:01 functions:01 int:01 int:01 partially:02 partially:02 arbitrary:02 arbitrary:02 argument:02 Hello Cedric, Thanks for your comments. Comments on your comments. > let rint () =3D (Random.bits () lsl 1) lxor (Random.bits ());; That was actually my first version. However I dropped it because I thought that generating a new random number by the interaction of the bits of two successive PRN could somehow change the underlying quality of the generator. Any thought ? > if Random.bool is more efficient than Random.bits, the latter seems the > best; to be benchmarked Random.bool is Random.bits () land 0 =3D 1. >> 2) Generate an arbitrary int in [0;max] (bounds included) >> >> let random_uint ?(max =3D max_int) =3D >> =C2=A0if max < 0 then invalid_arg "negative max" else >> =C2=A0if max =3D max_int then Random.bits else >> =C2=A0let bound =3D max + 1 in >> =C2=A0fun () -> Random.int bound > > Seems correct. > I don't know if use of exception is more efficient or not, it is to try: > > let random_uint ?(max =3D max_int) () =3D > =C2=A0try if max =3D max_int then Random.bits () else Random.int (max + 1= ) > =C2=A0with Invalid_argument "Random.int" -> invalid_arg "negative max" You lose the ability to partially apply random_uint with the max and avoid the if on each call. > I think it is better to use the bool trick; > simpler and doesn't use Int32: > > let random_int ?(max =3D max_int) =3D > =C2=A0 if max < 0 then invalid_arg "negative max" else > =C2=A0 if max =3D 0 then 0 else > =C2=A0 if Random.bool () > =C2=A0 then if max =3D max_int then Random.bits () > =C2=A0 =C2=A0 =C2=A0 =C2=A0else Random.int max > =C2=A0 else negate_minus_1 (Random.int (max - 1)) (* inline? *) > Yes, thanks. Your version can also be reordered to be partially applicable. It should also be noted that all our functions for [int]s don't work with the intended semantics on 64 bits platforms. >> 5) Generate an arbitrary float in [0;max] (bounds included) >> >> let after_one =3D 1. +. epsilon_float >> let random_ufloat ?(max =3D max_float) =3D >> =C2=A0if max < 0. then invalid_arg "negative max" else >> =C2=A0fun () -> (Random.float after_one) *. max > > This function is less interesting than Random.float: > only less or as many as values as floats in [0.;1.] can be generated > so you will miss many values only to be able to produce max > (for example, you cannot 1+eps if you try random_ufloat ~max:2. ()) The point you make maybe valid but unfortunately that's also the case for Random.float as this is the way it is implemented (generate a number in [0;1[ and scale it). Best, Daniel