Hugo Ferreira | 7 Nov 14:22
Favicon

Arbitrary precision random number generator

Hello,

Dose anyone know how I may generate a arbitrary precision random number.
The 64 bits is not enough. I looked at the Big_Int module but this
has no function for random number generation.

TIA,
Hugo F.

------------------------------------

Archives up to December 31, 2007 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ocaml_beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/ocaml_beginners/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:ocaml_beginners-digest <at> yahoogroups.com 
    mailto:ocaml_beginners-fullfeatured <at> yahoogroups.com

<*> To unsubscribe from this group, send an email to:
(Continue reading)

Jon Harrop | 7 Nov 15:37
Favicon

Re: Arbitrary precision random number generator

On Friday 07 November 2008 13:23:49 Hugo Ferreira wrote:
> Hello,
>
> Dose anyone know how I may generate a arbitrary precision random number.
> The 64 bits is not enough. I looked at the Big_Int module but this
> has no function for random number generation.

Call the 64-bit generator as many times as necessary and concatenate the bits 
in the results together to get a big random number.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e

------------------------------------

Archives up to December 31, 2007 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ocaml_beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/ocaml_beginners/join
    (Yahoo! ID required)

(Continue reading)

Hugo Ferreira | 7 Nov 15:05
Favicon

Re: Arbitrary precision random number generator

Jon Harrop wrote:
> On Friday 07 November 2008 13:23:49 Hugo Ferreira wrote:
>> Hello,
>>
>> Dose anyone know how I may generate a arbitrary precision random number.
>> The 64 bits is not enough. I looked at the Big_Int module but this
>> has no function for random number generation.
> 
> Call the 64-bit generator as many times as necessary and concatenate the bits 
> in the results together to get a big random number.
> 

Hmmm... that means I need to:
a) Find a way to generate a value within a range
b) Generating a Big_Int from all those values

Might be doable. Got a think some more.

Thanks,
Hugo F.

------------------------------------

Archives up to December 31, 2007 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ocaml_beginners/

(Continue reading)

Martin Jambon | 7 Nov 19:06
Favicon

Re: Arbitrary precision random number generator

Hugo Ferreira wrote:
> Jon Harrop wrote:
>> On Friday 07 November 2008 13:23:49 Hugo Ferreira wrote:
>>> Hello,
>>>
>>> Dose anyone know how I may generate a arbitrary precision random number.
>>> The 64 bits is not enough. I looked at the Big_Int module but this
>>> has no function for random number generation.
>> Call the 64-bit generator as many times as necessary and concatenate the bits 
>> in the results together to get a big random number.
>>
> 
> Hmmm... that means I need to:
> a) Find a way to generate a value within a range
> b) Generating a Big_Int from all those values
> 
> Might be doable. Got a think some more.

Here is some code that generates a random string. Not sure how much this
is reusable to make a big int.

$ cat random_extras.ml
module State =
struct
  include Random.State

  let string state n =
    let s = String.create n in
    for i = 0 to n / 3 - 1 do
      let x = Random.State.bits state in
(Continue reading)

Hugo Ferreira | 7 Nov 20:49
Favicon

Re: Arbitrary precision random number generator

To William, Eric, Dario, rixed and Martin

Thanks for the suggestions.

After some consideration I have opted not to dabble with
random number generation. This is because I don't have the
required knowledge to ensure that the numbers I do generate
have the necessary characteristics. Because I want to generate
and use (add, subtract, etc.) arbitrary precision numbers, I
will stick to a library that provides this.

Just in case someone else requires this info, I am posting
here some other options I've considered:

a) Convert the functions in Random using the Nat module
b) Numerix Ocaml library (http://pauillac.inria.fr/~quercia/)
c) MLGMP (http://www-verimag.imag.fr/~monniaux/index.html.en)
d) MLGMPIDL
    (http://pop-art.inrialpes.fr/people/bjeannet/mlxxxidl-forge/)

The best solution of course would be if Big_int provided the
functionality (maybe using option a?). Second best seems to be
Numerix, however it is not as portable. Third best is MLGMPIDL
which basically offers what MLGMPIDL does. As for MLGMP, it
looks like it has been shelved (2002).

I will (unless something else crops up) be using MLGMPIDL as I am
also using the authors MLCuDDIDl library. I wanted to reduce my
dependencies on external libraries however this provides me with
necessary functionality.
(Continue reading)

William D. Neumann | 12 Nov 16:55

Re: Arbitrary precision random number generator

On Fri, 07 Nov 2008 19:49:59 +0000, Hugo Ferreira wrote
> To William, Eric, Dario, rixed and Martin
>
> Thanks for the suggestions.
>
> After some consideration I have opted not to dabble with
> random number generation. This is because I don't have the
> required knowledge to ensure that the numbers I do generate
> have the necessary characteristics. Because I want to generate
> and use (add, subtract, etc.) arbitrary precision numbers, I
> will stick to a library that provides this.

What characteristics do you need?

--

William D. Neumann

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

New business?

Get new customers.

List your web site

in Yahoo! Search.

Special K Group

on Yahoo! Groups

Learn how others

are losing pounds.

.

__,_._,___
Hugo Ferreira | 12 Nov 20:37
Favicon

Re: Arbitrary precision random number generator


William D. Neumann wrote:
> On Fri, 07 Nov 2008 19:49:59 +0000, Hugo Ferreira wrote
>> To William, Eric, Dario, rixed and Martin
>>
>> Thanks for the suggestions.
>>
>> After some consideration I have opted not to dabble with
>> random number generation. This is because I don't have the
>> required knowledge to ensure that the numbers I do generate
>> have the necessary characteristics. Because I want to generate
>> and use (add, subtract, etc.) arbitrary precision numbers, I
>> will stick to a library that provides this.
>
> What characteristics do you need?
>

I want to use this random generator to test a machine learning
algorithm. I must therefore be careful not to introduce any
unwanted bias. So I need a random generator that generates
"good" random sequences.

Hope that makes sense.

> --
>
> William D. Neumann
>
>
> ------------------------------------
>
> Archives up to December 31, 2007 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
>
>
>

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

New business?

Get new customers.

List your web site

in Yahoo! Search.

John McEnroe

on Yahoo! Groups

Join him for the

10 Day Challenge.

.

__,_._,___
William D. Neumann | 7 Nov 16:51

Re: Arbitrary precision random number generator

Or modify the interface to Random. Or, even easier, use another PRNG that's
easy to pull bits from. E.g. here's a simple implementation of RC4 (written
more for cleanliness than performance, you can tweak this, or use Cryptokit
to use a faster C based version if you need speed).

external get_byte : string -> int -> int = "%string_unsafe_get"
external set_byte : string -> int -> int -> unit = "%string_unsafe_set"

class rc4 =
object (self)
val state = Array.init 256 (fun i -> i)
val mutable x = 0
val mutable y = 0

method private swap i j =
let t = state.(i) in state.(i) <- state.(j); state.(j) <- t

method private mask =
x <- (x+1) land 0xff;
y <- ((y + state.(x)) land 0xff);
self#swap x y;
state.((state.(x) + state.(y)) land 0xff)

method init key =
let state_index = ref 0
and len = String.length key in
for i = 0 to 255 do
state_index := ((!state_index + state.(i) + (get_byte key (i mod
len)))) land 0xff;
self#swap i !state_index
done

method crypt src =
let dest = String.copy src in
for i = 0 to String.length dest - 1 do
set_byte dest i ((get_byte dest i) lxor self#mask)
done;
dest

method gen_bytes num_bytes =
let r = String.create num_bytes in
for i = 0 to num_bytes - 1 do set_byte r i self#mask done;
r
end

-------------------

# let prng = new rc4;;
val prng : rc4 = <obj>
# prng#init "this is a simple seed";;
- : unit = ()
# let random_bits = prng#gen_bytes 100;;
val random_bits : string =
"÷­N´öjæ-’á§\007iÔãb)öø’úÓÕw¨5Y}d8«\1579RE\007ŽHƒ6Ý\143Ã[\153T)«µ\136¢)!
ÿ܇”\152àI—âÓ/“eò©á\025´ØXÃÿ²î–‰Îtr\018H–Á‡ãê_\019MøÞ+\008\028ê\007\143"

On Fri, 7 Nov 2008 14:37:07 +0000, Jon Harrop wrote
> On Friday 07 November 2008 13:23:49 Hugo Ferreira wrote:
> > Hello,
> >
> > Dose anyone know how I may generate a arbitrary precision random number.
> > The 64 bits is not enough. I looked at the Big_Int module but this
> > has no function for random number generation.
>
> Call the 64-bit generator as many times as necessary and concatenate
> the bits in the results together to get a big random number.
>
> --
> Dr Jon Harrop, Flying Frog Consultancy Ltd.
> http://www.ffconsultancy.com/?e
>
> ------------------------------------
>
> Archives up to December 31, 2007 are also downloadable at
http://www.connettivo.net/cntprojects/ocaml_beginners/
> The archives of the very official ocaml list (the seniors' one) can
> be found at http://caml.inria.fr Attachments are banned and you're
> asked to be polite, avoid flames etc.Yahoo! Groups Links
>
>
>

--

William D. Neumann

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Search Ads

Get new customers.

List your web site

in Yahoo! Search.

Yahoo! Groups

w/ John McEnroe

Join the All-Bran

Day 10 Club.

.

__,_._,___
Eric Cooper | 7 Nov 16:59

Re: Arbitrary precision random number generator

On Fri, Nov 07, 2008 at 01:23:49PM +0000, Hugo Ferreira wrote:
> Dose anyone know how I may generate a arbitrary precision random number.
> The 64 bits is not enough. I looked at the Big_Int module but this
> has no function for random number generation.

On a Linux platform, you can read bytes from /dev/urandom
(and get true random numbers instead of pseudo-random ones).

--
Eric Cooper e c c <at> c m u . e d u

__._,_.___
Recent Activity
Visit Your Group
Give Back

Yahoo! for Good

Get inspired

by a good cause.

Y! Toolbar

Get it Free!

easy 1-click access

to your groups.

Yahoo! Groups

Start a group

in 3 easy steps.

Connect with others.

.

__,_._,___
Dario Teixeira | 7 Nov 17:25
Favicon

Re: Arbitrary precision random number generator

> On a Linux platform, you can read bytes from /dev/urandom
> (and get true random numbers instead of pseudo-random ones).
^^^^

And by "true", you mean "also pseudo though generally better",
of course... :-)

Cheers,
Dario Teixeira

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

New web site?

Drive traffic now.

Get your business

on Yahoo! search.

Yahoo! Groups

Going Green Zone

Learn to go green.

Save energy. Save the planet.

.

__,_._,___
rixed | 7 Nov 18:13
Gravatar

Re: Arbitrary precision random number generator

-[ Fri, Nov 07, 2008 at 10:59:20AM -0500, Eric Cooper ]----
> On a Linux platform, you can read bytes from /dev/urandom
> (and get true random numbers instead of pseudo-random ones).

Last time I checked you coul not read many of these. It's
good for an initial seed, not a long sequence.

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Need traffic?

Drive customers

With search ads

on Yahoo!

Yahoo! Groups

Join people over 40

who are finding ways

to stay in shape.

.

__,_._,___
Dario Teixeira | 7 Nov 18:34
Favicon

Re: Arbitrary precision random number generator

> > On a Linux platform, you can read bytes from /dev/urandom
> > (and get true random numbers instead of pseudo-random ones).
>
> Last time I checked you coul not read many of these.
> It's good for an initial seed, not a long sequence.

You can read as many as you wish from /dev/urandom.
/dev/random is a different matter, however (it will
block waiting for entropy when there's not enough).

Cheers,
Dario

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Yahoo! Groups

Cat Owners Group

Join a community

for cat lovers

Best of Y! Groups

Discover groups

that are the best

of their class.

.

__,_._,___
rixed | 7 Nov 21:20
Gravatar

Re: Arbitrary precision random number generator

Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

New web site?

Drive traffic now.

Get your business

on Yahoo! search.

Yahoo! Groups

Special K Challenge

Join others who

are losing pounds.

.

__,_._,___
Florent Monnier | 9 Nov 20:39
X-Face

Re: Arbitrary precision random number generator

> You can read as many as you wish from /dev/urandom.
> /dev/random is a different matter, however (it will
> block waiting for entropy when there's not enough).

when you read more data from /dev/random what you can do to reduce the time 
needed to get the data is to ask the user to move the mouse

out of topic, just to play you can do a numeric niagara falls by:
cat /dev/urandom | xxd

you can also use this trick to compare /dev/random with and without the mouse 
moving

it's also a good way to get a good idea of the difference between random and 
urandom flows

------------------------------------

Archives up to December 31, 2007 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ocaml_beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/ocaml_beginners/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:ocaml_beginners-digest <at> yahoogroups.com 
    mailto:ocaml_beginners-fullfeatured <at> yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe <at> yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/


Gmane