From: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
To: AUGER <Cedric.Auger@lri.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Random questions
Date: Thu, 3 Dec 2009 22:48:37 +0800 [thread overview]
Message-ID: <91a3da520912030648u62aac252g6faddf06892646d9@mail.gmail.com> (raw)
In-Reply-To: <op.u4czjxj9l4bj1b@lri4-139>
Hello Cedric,
Thanks for your comments. Comments on your comments.
> let rint () = (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 = 1.
>> 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
>
> Seems correct.
> I don't know if use of exception is more efficient or not, it is to try:
>
> let random_uint ?(max = max_int) () =
> try if max = max_int then Random.bits () else Random.int (max + 1)
> with 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 = max_int) =
> if max < 0 then invalid_arg "negative max" else
> if max = 0 then 0 else
> if Random.bool ()
> then if max = max_int then Random.bits ()
> else Random.int max
> 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 = 1. +. epsilon_float
>> let random_ufloat ?(max = max_float) =
>> if max < 0. then invalid_arg "negative max" else
>> fun () -> (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
next prev parent reply other threads:[~2009-12-03 14:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-18 10:59 Daniel Bünzli
2009-12-03 11:00 ` [Caml-list] " AUGER
2009-12-03 14:48 ` Daniel Bünzli [this message]
2009-12-03 15:11 ` AUGER
2009-12-03 16:01 ` Damien Doligez
2009-12-03 16:45 ` AUGER
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=91a3da520912030648u62aac252g6faddf06892646d9@mail.gmail.com \
--to=daniel.buenzli@erratique.ch \
--cc=Cedric.Auger@lri.fr \
--cc=caml-list@inria.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