Jsoftware
High-Performance Development Platform

Random number generation

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

Random number generation

by Benoît Roesslinger :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am new to J and when doing some experiments with random number generation
I stumbled across the following behavior, which wasn't what I'd expect :

   f=: 3 : 'y * ?0'
   f 3
2.91414
   f 3
0.139888
   f 3
0.990328

OK so far, but when I tried:

   f 3 3

it gives me :

0.0403801 0.0403801 (same values!)

whereas I'd expect a behavior much like the one of '?'...
Is this behavior normal ?
Suppose I want to create a function to generate a random deviate from a
distribution (normal for instance) with some parameters (mean and sd for
instance) that will work in the same fashion as '?', ie it is possible to
generate lots of random deviates at once using code such as : distri 100 $
x, where x would represent parameters, what is the best way to go ?

Many thanks in advance!

Benoît.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Random number generation

by Brian Schott :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You need f=:     3 : 'y * ?(#y)#0'2 3


because ?0 produces a scalar result.

On Mon, Jun 30, 2008 at 10:46 AM, Benoît Roesslinger <broessli@...> wrote:

> Hi,
>
> I am new to J and when doing some experiments with random number generation
> I stumbled across the following behavior, which wasn't what I'd expect :
>
>   f=: 3 : 'y * ?0'
>   f 3
> 2.91414
>   f 3
> 0.139888
>   f 3
> 0.990328
>
> OK so far, but when I tried:
>
>   f 3 3
>
> it gives me :
>
> 0.0403801 0.0403801 (same values!)
>
> whereas I'd expect a behavior much like the one of '?'...
> Is this behavior normal ?
> Suppose I want to create a function to generate a random deviate from a
> distribution (normal for instance) with some parameters (mean and sd for
> instance) that will work in the same fashion as '?', ie it is possible to
> generate lots of random deviates at once using code such as : distri 100 $
> x, where x would represent parameters, what is the best way to go ?
>
> Many thanks in advance!
>
> Benoît.
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>



--
(B=) <-----my sig
Brian Schott
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Random number generation

by Brian Schott :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Correction.
  f=:  3 : 'y * ?(#y)#0'
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

RE: Random number generation

by Henry Rich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

What you executed was:

3 3 * ?0

which is

3 3 * (?0)

in other words, you asked for one number, then multiplied it by 3 twice.


You could have your verb apply to atoms by giving it a rank of 0:

   f1 =: 3 : 'y * ?0'"0

Or, you could design it to work with arrays of any shape:

   f2 =: 3 : 'y * ?(#y)#0'

Working with bigger arrays is faster, but perhaps not by enough
to be worth the trouble.  Depends on your application.

Henry Rich
 

> -----Original Message-----
> From: programming-bounces@...
> [mailto:programming-bounces@...] On Behalf Of
> Benoît Roesslinger
> Sent: Monday, June 30, 2008 10:46 AM
> To: programming@...
> Subject: [Jprogramming] Random number generation
>
> Hi,
>
> I am new to J and when doing some experiments with random
> number generation
> I stumbled across the following behavior, which wasn't what
> I'd expect :
>
>    f=: 3 : 'y * ?0'
>    f 3
> 2.91414
>    f 3
> 0.139888
>    f 3
> 0.990328
>
> OK so far, but when I tried:
>
>    f 3 3
>
> it gives me :
>
> 0.0403801 0.0403801 (same values!)
>
> whereas I'd expect a behavior much like the one of '?'...
> Is this behavior normal ?
> Suppose I want to create a function to generate a random
> deviate from a
> distribution (normal for instance) with some parameters (mean
> and sd for
> instance) that will work in the same fashion as '?', ie it is
> possible to
> generate lots of random deviates at once using code such as :
> distri 100 $
> x, where x would represent parameters, what is the best way to go ?
>
> Many thanks in advance!
>
> Benoît.
> ----------------------------------------------------------------------
> For information about J forums see
> http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

RE: Random number generation

by Joey K Tuttle :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Not to quibble... but

    f2 =: 3 : 'y * ?(#y)#0'

has a related trouble to the original post with
argument rank > 1 ... f1 is actually better in
extending "to work with arrays of any shape". To
try to gain some speed, perhaps you really meant
to say:

    f2 =: 3 : 'y * ?($y)$0'

- joey


At 10:59  -0400 2008/06/30, Henry Rich wrote:

>What you executed was:
>
>3 3 * ?0
>
>which is
>
>3 3 * (?0)
>
>in other words, you asked for one number, then multiplied it by 3 twice.
>
>
>You could have your verb apply to atoms by giving it a rank of 0:
>
>    f1 =: 3 : 'y * ?0'"0
>
>Or, you could design it to work with arrays of any shape:
>
>    f2 =: 3 : 'y * ?(#y)#0'
>
>Working with bigger arrays is faster, but perhaps not by enough
>to be worth the trouble.  Depends on your application.
>
>Henry Rich
>
>
>>  -----Original Message-----
>>  From: programming-bounces@...
>>  [mailto:programming-bounces@...] On Behalf Of
>>  Benoît Roesslinger
>>  Sent: Monday, June 30, 2008 10:46 AM
>>  To: programming@...
>>  Subject: [Jprogramming] Random number generation
>>
>>  Hi,
>>
>>  I am new to J and when doing some experiments with random
>>  number generation
>>  I stumbled across the following behavior, which wasn't what
>>  I'd expect :
>>
>>     f=: 3 : 'y * ?0'
>>     f 3
>>  2.91414
>>     f 3
>>  0.139888
>>     f 3
>>  0.990328
>>
>>  OK so far, but when I tried:
>>
>>     f 3 3
>>
>>  it gives me :
>>
>>  0.0403801 0.0403801 (same values!)
>>
>>  whereas I'd expect a behavior much like the one of '?'...
>>  Is this behavior normal ?
>>  Suppose I want to create a function to generate a random
>>  deviate from a
>>  distribution (normal for instance) with some parameters (mean
>>  and sd for
>>  instance) that will work in the same fashion as '?', ie it is
>>  possible to
>>  generate lots of random deviates at once using code such as :
>>  distri 100 $
>>  x, where x would represent parameters, what is the best way to go ?
>>
>>  Many thanks in advance!
>>
>>  Benoît.
>>  ----------------------------------------------------------------------
>>  For information about J forums see
>>  http://www.jsoftware.com/forums.htm
>
>----------------------------------------------------------------------
>For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Random number generation

by Roger Hui :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

To gain more speed (and reduce space), use:

f3=: 3 : 'y * ($y) ?@$ 0'

?@$ is supported by special code.  The difference
is most striking for random booleans:

   ts=: 6!:2 , 7!:2@]
   
   ts '1e6 ?@$ 2'
0.00275063 1.04934e6
   ts '?1e6$2'
0.00555685 5.24352e6



----- Original Message -----
From: Joey K Tuttle <jkt@...>
Date: Monday, June 30, 2008 8:54
Subject: RE: [Jprogramming] Random number generation
To: Programming forum <programming@...>
Cc: 'Programming forum' <programming@...>

> Not to quibble... but
>
>     f2 =: 3 : 'y * ?(#y)#0'
>
> has a related trouble to the original post with
> argument rank > 1 ... f1 is actually better in
> extending "to work with arrays of any shape". To
> try to gain some speed, perhaps you really meant
> to say:
>
>     f2 =: 3 : 'y * ?($y)$0'
>
> - joey
>
>
> At 10:59  -0400 2008/06/30, Henry Rich wrote:
> >What you executed was:
> >
> >3 3 * ?0
> >
> >which is
> >
> >3 3 * (?0)
> >
> >in other words, you asked for one number, then multiplied it by
> 3 twice.
> >
> >
> >You could have your verb apply to atoms by giving it a rank of 0:
> >
> >    f1 =: 3 : 'y * ?0'"0
> >
> >Or, you could design it to work with arrays of any shape:
> >
> >    f2 =: 3 : 'y * ?(#y)#0'
> >
> >Working with bigger arrays is faster, but perhaps not by enough
> >to be worth the trouble.  Depends on your application.
> >
> >Henry Rich
> >
> >
> >>  -----Original Message-----
> >>  From: programming-bounces@...
> >>  [mailto:programming-bounces@...] On Behalf Of
> >>  Benoît Roesslinger
> >>  Sent: Monday, June 30, 2008 10:46 AM
> >>  To: programming@...
> >>  Subject: [Jprogramming] Random number generation
> >>
> >>  Hi,
> >>
> >>  I am new to J and when doing some experiments with random
> >>  number generation
> >>  I stumbled across the following behavior, which wasn't what
> >>  I'd expect :
> >>
> >>     f=: 3 : 'y * ?0'
> >>     f 3
> >>  2.91414
> >>     f 3
> >>  0.139888
> >>     f 3
> >>  0.990328
> >>
> >>  OK so far, but when I tried:
> >>
> >>     f 3 3
> >>
> >>  it gives me :
> >>
> >>  0.0403801 0.0403801 (same values!)
> >>
> >>  whereas I'd expect a behavior much like the one of '?'...
> >>  Is this behavior normal ?
> >>  Suppose I want to create a function to generate a random
> >>  deviate from a
> >>  distribution (normal for instance) with some parameters (mean
> >>  and sd for
> >>  instance) that will work in the same fashion as '?', ie
> it is
> >>  possible to
> >>  generate lots of random deviates at once using code
> such as :
> >>  distri 100 $
> >>  x, where x would represent parameters, what is the best
> way to go ?
> >>
> >>  Many thanks in advance!
> >>
> >>  Benoît.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

RE: Random number generation

by Henry Rich :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Oops, you got me.

Henry Rich

> -----Original Message-----
> From: programming-bounces@...
> [mailto:programming-bounces@...] On Behalf Of Joey K Tuttle
> Sent: Monday, June 30, 2008 11:54 AM
> To: Programming forum
> Cc: 'Programming forum'
> Subject: RE: [Jprogramming] Random number generation
>
> Not to quibble... but
>
>     f2 =: 3 : 'y * ?(#y)#0'
>
> has a related trouble to the original post with
> argument rank > 1 ... f1 is actually better in
> extending "to work with arrays of any shape". To
> try to gain some speed, perhaps you really meant
> to say:
>
>     f2 =: 3 : 'y * ?($y)$0'
>
> - joey
>
>
> At 10:59  -0400 2008/06/30, Henry Rich wrote:
> >What you executed was:
> >
> >3 3 * ?0
> >
> >which is
> >
> >3 3 * (?0)
> >
> >in other words, you asked for one number, then multiplied it
> by 3 twice.
> >
> >
> >You could have your verb apply to atoms by giving it a rank of 0:
> >
> >    f1 =: 3 : 'y * ?0'"0
> >
> >Or, you could design it to work with arrays of any shape:
> >
> >    f2 =: 3 : 'y * ?(#y)#0'
> >
> >Working with bigger arrays is faster, but perhaps not by enough
> >to be worth the trouble.  Depends on your application.
> >
> >Henry Rich
> >
> >
> >>  -----Original Message-----
> >>  From: programming-bounces@...
> >>  [mailto:programming-bounces@...] On Behalf Of
> >>  Benoît Roesslinger
> >>  Sent: Monday, June 30, 2008 10:46 AM
> >>  To: programming@...
> >>  Subject: [Jprogramming] Random number generation
> >>
> >>  Hi,
> >>
> >>  I am new to J and when doing some experiments with random
> >>  number generation
> >>  I stumbled across the following behavior, which wasn't what
> >>  I'd expect :
> >>
> >>     f=: 3 : 'y * ?0'
> >>     f 3
> >>  2.91414
> >>     f 3
> >>  0.139888
> >>     f 3
> >>  0.990328
> >>
> >>  OK so far, but when I tried:
> >>
> >>     f 3 3
> >>
> >>  it gives me :
> >>
> >>  0.0403801 0.0403801 (same values!)
> >>
> >>  whereas I'd expect a behavior much like the one of '?'...
> >>  Is this behavior normal ?
> >>  Suppose I want to create a function to generate a random
> >>  deviate from a
> >>  distribution (normal for instance) with some parameters (mean
> >>  and sd for
> >>  instance) that will work in the same fashion as '?', ie it is
> >>  possible to
> >>  generate lots of random deviates at once using code such as :
> >>  distri 100 $
> >>  x, where x would represent parameters, what is the best
> way to go ?
> >>
> >>  Many thanks in advance!
> >>
> >>  Benoît.
> >>  
> ----------------------------------------------------------------------
> >>  For information about J forums see
> >>  http://www.jsoftware.com/forums.htm
> >
> >-------------------------------------------------------------
> ---------
> >For information about J forums see
> http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see
> http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Random number generation

by Joey K Tuttle :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Interestingly, I see almost no change in speed/space between (my) f2 and f3...

    ts 'f2 1000 1000$9'
0.231313 2.20224e7
    ts 'f3 1000 1000$9'
0.235067 2.0974e7

Well - I guess a million bytes in space saving is
nothing to sneeze at (used to be a lot of
memory...) so I applaud the optimization work you
have done!

Thanks - joey

At 10:08  -0700 2008/06/30, Roger Hui wrote:

>To gain more speed (and reduce space), use:
>
>f3=: 3 : 'y * ($y) ?@$ 0'
>
>?@$ is supported by special code.  The difference
>is most striking for random booleans:
>
>    ts=: 6!:2 , 7!:2@]
>
>    ts '1e6 ?@$ 2'
>0.00275063 1.04934e6
>    ts '?1e6$2'
>0.00555685 5.24352e6
>
>
>
>----- Original Message -----
>From: Joey K Tuttle <jkt@...>
>Date: Monday, June 30, 2008 8:54
>Subject: RE: [Jprogramming] Random number generation
>To: Programming forum <programming@...>
>Cc: 'Programming forum' <programming@...>
>
>>  Not to quibble... but
>>
>>      f2 =: 3 : 'y * ?(#y)#0'
>>
>>  has a related trouble to the original post with
>>  argument rank > 1 ... f1 is actually better in
>>  extending "to work with arrays of any shape". To
>>  try to gain some speed, perhaps you really meant
>>  to say:
>>
>>      f2 =: 3 : 'y * ?($y)$0'
>>
>>  - joey
>>
>>
>>  At 10:59  -0400 2008/06/30, Henry Rich wrote:
>>  >What you executed was:
>>  >
>>  >3 3 * ?0
>>  >
>>  >which is
>>  >
>>  >3 3 * (?0)
>>  >
>>  >in other words, you asked for one number, then multiplied it by
>>  3 twice.
>>  >
>>  >
>>  >You could have your verb apply to atoms by giving it a rank of 0:
>>  >
>>  >    f1 =: 3 : 'y * ?0'"0
>>  >
>>  >Or, you could design it to work with arrays of any shape:
>>  >
>>  >    f2 =: 3 : 'y * ?(#y)#0'
>>  >
>>  >Working with bigger arrays is faster, but perhaps not by enough
>>  >to be worth the trouble.  Depends on your application.
>>  >
>>  >Henry Rich
>>  >
>>  >
>>  >>  -----Original Message-----
>>  >>  From: programming-bounces@...
>>  >>  [mailto:programming-bounces@...] On Behalf Of
>>  >>  Benoît Roesslinger
>>  >>  Sent: Monday, June 30, 2008 10:46 AM
>>  >>  To: programming@...
>>  >>  Subject: [Jprogramming] Random number generation
>>  >>
>>  >>  Hi,
>>  >>
>>  >>  I am new to J and when doing some experiments with random
>>  >>  number generation
>>  >>  I stumbled across the following behavior, which wasn't what
>>  >>  I'd expect :
>>  >>
>>  >>     f=: 3 : 'y * ?0'
>>  >>     f 3
>>  >>  2.91414
>>  >>     f 3
>>  >>  0.139888
>>  >>     f 3
>>  >>  0.990328
>>  >>
>>  >>  OK so far, but when I tried:
>>  >>
>>  >>     f 3 3
>>  >>
>>  >>  it gives me :
>>  >>
>>  >>  0.0403801 0.0403801 (same values!)
>>  >>
>>  >>  whereas I'd expect a behavior much like the one of '?'...
>>  >>  Is this behavior normal ?
>>  >>  Suppose I want to create a function to generate a random
>>  >>  deviate from a
>>  >>  distribution (normal for instance) with some parameters (mean
>>  >>  and sd for
>>  >>  instance) that will work in the same fashion as '?', ie
>>  it is
>>  >>  possible to
>>  >>  generate lots of random deviates at once using code
>>  such as :
>>  >>  distri 100 $
>>  >>  x, where x would represent parameters, what is the best
>>  way to go ?
>>  >>
>>  >>  Many thanks in advance!
>>  >>
>>  >>  Benoît.
>----------------------------------------------------------------------
>For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

RE: Random number generation

by R.E. Boss :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

   f3=: 3 : 'y * ($y) ?@$ 0'
   f2=: 3 : 'y * ?(#y)#0'

   ts 'f2 Y' [Y=:1000 1000$9
0.0098060407 8402944
   ts 'f3 Y'
0.034617353 16781696


R.E. Boss


> -----Oorspronkelijk bericht-----
> Van: programming-bounces@... [mailto:programming-
> bounces@...] Namens Joey K Tuttle
> Verzonden: maandag 30 juni 2008 19:41
> Aan: Programming forum
> Onderwerp: Re: [Jprogramming] Random number generation
>
> Interestingly, I see almost no change in speed/space between (my) f2 and
> f3...
>
>     ts 'f2 1000 1000$9'
> 0.231313 2.20224e7
>     ts 'f3 1000 1000$9'
> 0.235067 2.0974e7
>
> Well - I guess a million bytes in space saving is
> nothing to sneeze at (used to be a lot of
> memory...) so I applaud the optimization work you
> have done!
>
> Thanks - joey
>
> At 10:08  -0700 2008/06/30, Roger Hui wrote:
> >To gain more speed (and reduce space), use:
> >
> >f3=: 3 : 'y * ($y) ?@$ 0'
> >
> >?@$ is supported by special code.  The difference
> >is most striking for random booleans:
> >
> >    ts=: 6!:2 , 7!:2@]
> >
> >    ts '1e6 ?@$ 2'
> >0.00275063 1.04934e6
> >    ts '?1e6$2'
> >0.00555685 5.24352e6
> >
> >
> >
> >----- Original Message -----
> >From: Joey K Tuttle <jkt@...>
> >Date: Monday, June 30, 2008 8:54
> >Subject: RE: [Jprogramming] Random number generation
> >To: Programming forum <programming@...>
> >Cc: 'Programming forum' <programming@...>
> >
> >>  Not to quibble... but
> >>
> >>      f2 =: 3 : 'y * ?(#y)#0'
> >>
> >>  has a related trouble to the original post with
> >>  argument rank > 1 ... f1 is actually better in
> >>  extending "to work with arrays of any shape". To
> >>  try to gain some speed, perhaps you really meant
> >>  to say:
> >>
> >>      f2 =: 3 : 'y * ?($y)$0'
> >>
> >>  - joey
> >>
> >>
> >>  At 10:59  -0400 2008/06/30, Henry Rich wrote:
> >>  >What you executed was:
> >>  >
> >>  >3 3 * ?0
> >>  >
> >>  >which is
> >>  >
> >>  >3 3 * (?0)
> >>  >
> >>  >in other words, you asked for one number, then multiplied it by
> >>  3 twice.
> >>  >
> >>  >
> >>  >You could have your verb apply to atoms by giving it a rank of 0:
> >>  >
> >>  >    f1 =: 3 : 'y * ?0'"0
> >>  >
> >>  >Or, you could design it to work with arrays of any shape:
> >>  >
> >>  >    f2 =: 3 : 'y * ?(#y)#0'
> >>  >
> >>  >Working with bigger arrays is faster, but perhaps not by enough
> >>  >to be worth the trouble.  Depends on your application.
> >>  >
> >>  >Henry Rich
> >>  >
> >>  >
> >>  >>  -----Original Message-----
> >>  >>  From: programming-bounces@...
> >>  >>  [mailto:programming-bounces@...] On Behalf Of
> >>  >>  Benoît Roesslinger
> >>  >>  Sent: Monday, June 30, 2008 10:46 AM
> >>  >>  To: programming@...
> >>  >>  Subject: [Jprogramming] Random number generation
> >>  >>
> >>  >>  Hi,
> >>  >>
> >>  >>  I am new to J and when doing some experiments with random
> >>  >>  number generation
> >>  >>  I stumbled across the following behavior, which wasn't what
> >>  >>  I'd expect :
> >>  >>
> >>  >>     f=: 3 : 'y * ?0'
> >>  >>     f 3
> >>  >>  2.91414
> >>  >>     f 3
> >>  >>  0.139888
> >>  >>     f 3
> >>  >>  0.990328
> >>  >>
> >>  >>  OK so far, but when I tried:
> >>  >>
> >>  >>     f 3 3
> >>  >>
> >>  >>  it gives me :
> >>  >>
> >>  >>  0.0403801 0.0403801 (same values!)
> >>  >>
> >>  >>  whereas I'd expect a behavior much like the one of '?'...
> >>  >>  Is this behavior normal ?
> >>  >>  Suppose I want to create a function to generate a random
> >>  >>  deviate from a
> >>  >>  distribution (normal for instance) with some parameters (mean
> >>  >>  and sd for
> >>  >>  instance) that will work in the same fashion as '?', ie
> >>  it is
> >>  >>  possible to
> >>  >>  generate lots of random deviates at once using code
> >>  such as :
> >>  >>  distri 100 $
> >>  >>  x, where x would represent parameters, what is the best
> >>  way to go ?
> >>  >>
> >>  >>  Many thanks in advance!
> >>  >>
> >>  >>  Benoît.
> >----------------------------------------------------------------------
> >For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Random number generation

by Raul Miller-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 7/1/08, R.E. Boss <r.e.boss@...> wrote:
>   f3=: 3 : 'y * ($y) ?@$ 0'
>   f2=: 3 : 'y * ?(#y)#0'
>
>   ts 'f2 Y' [Y=:1000 1000$9
> 0.0098060407 8402944
>   ts 'f3 Y'
> 0.034617353 16781696

ts=: 6!:2, 7!:2&]
f2=: 3 : 'y * ?(#y)#0'
f3=: 3 : 'y * ($y) ?@$ 0'
Y=: 1000 1000$9

NB. J64, 3GHz CPU
   ts 'f2 Y'
0.00713778 8.40294e6
   ts 'f3 Y'
0.0262947 1.67817e7

NB. J32, 1.8GHz CPU
   ts 'f2 Y'
0.0746899 1.78279e7
   ts 'f3 Y'
0.0724159 1.67795e7

Note also that times can vary by more than 25%

FYI,

--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Random number generation

by Roger Hui :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Timing is not everything.  In this case f2 is computing
something weird and possibly misleading:  All the
elements of a row are the same. e.g.

   f2 3 4$9
2.06843 2.06843 2.06843 2.06843
5.20443 5.20443 5.20443 5.20443
1.00311 1.00311 1.00311 1.00311
   f3 3 4$9
 3.36383 8.62184  6.76132 4.76392
 8.01329 7.10008 0.285073 3.84514
0.950537 6.16313 0.214141 3.56592



----- Original Message -----
From: "R.E. Boss" <r.e.boss@...>
Date: Monday, June 30, 2008 23:56
Subject: RE: [Jprogramming] Random number generation
To: 'Programming forum' <programming@...>

>    f3=: 3 : 'y * ($y) ?@$ 0'
>    f2=: 3 : 'y * ?(#y)#0'
>
>    ts 'f2 Y' [Y=:1000 1000$9
> 0.0098060407 8402944
>    ts 'f3 Y'
> 0.034617353 16781696
>
>
> R.E. Boss
>
>
> > -----Oorspronkelijk bericht-----
> > Van: programming-bounces@... [mailto:programming-
> > bounces@...] Namens Joey K Tuttle
> > Verzonden: maandag 30 juni 2008 19:41
> > Aan: Programming forum
> > Onderwerp: Re: [Jprogramming] Random number generation
> >
> > Interestingly, I see almost no change in speed/space between
> (my) f2 and
> > f3...
> >
> >     ts 'f2 1000 1000$9'
> > 0.231313 2.20224e7
> >     ts 'f3 1000 1000$9'
> > 0.235067 2.0974e7
> >
> > Well - I guess a million bytes in space saving is
> > nothing to sneeze at (used to be a lot of
> > memory...) so I applaud the optimization work you
> > have done!
> >
> > Thanks - joey
> >
> > At 10:08  -0700 2008/06/30, Roger Hui wrote:
> > >To gain more speed (and reduce space), use:
> > >
> > >f3=: 3 : 'y * ($y) ?@$ 0'
> > >
> > >?@$ is supported by special code.  The difference
> > >is most striking for random booleans:
> > >
> > >    ts=: 6!:2 , 7!:2@]
> > >
> > >    ts '1e6 ?@$ 2'
> > >0.00275063 1.04934e6
> > >    ts '?1e6$2'
> > >0.00555685 5.24352e6
> > >
> > >
> > >
> > >----- Original Message -----
> > >From: Joey K Tuttle <jkt@...>
> > >Date: Monday, June 30, 2008 8:54
> > >Subject: RE: [Jprogramming] Random number generation
> > >To: Programming forum <programming@...>
> > >Cc: 'Programming forum' <programming@...>
> > >
> > >>  Not to quibble... but
> > >>
> > >>      f2 =: 3 : 'y * ?(#y)#0'
> > >>
> > >>  has a related trouble to the original post with
> > >>  argument rank > 1 ... f1 is actually better in
> > >>  extending "to work with arrays of any shape". To
> > >>  try to gain some speed, perhaps you really meant
> > >>  to say:
> > >>
> > >>      f2 =: 3 : 'y * ?($y)$0'
> > >>
> > >>  - joey
> > >>
> > >>
> > >>  At 10:59  -0400 2008/06/30, Henry Rich wrote:
> > >>  >What you executed was:
> > >>  >
> > >>  >3 3 * ?0
> > >>  >
> > >>  >which is
> > >>  >
> > >>  >3 3 * (?0)
> > >>  >
> > >>  >in other words, you asked for one number, then
> multiplied it by
> > >>  3 twice.
> > >>  >
> > >>  >
> > >>  >You could have your verb apply to atoms by giving it
> a rank of 0:
> > >>  >
> > >>  >    f1 =: 3 : 'y * ?0'"0
> > >>  >
> > >>  >Or, you could design it to work with arrays of any shape:
> > >>  >
> > >>  >    f2 =: 3 : 'y * ?(#y)#0'
> > >>  >
> > >>  >Working with bigger arrays is faster, but perhaps
> not by enough
> > >>  >to be worth the trouble.  Depends on your
> application.> >>  >
> > >>  >Henry Rich
> > >>  >
> > >>  >
> > >>  >>  -----Original Message-----
> > >>  >>  From: programming-bounces@...
> > >>  >>  [mailto:programming-bounces@...]
> On Behalf Of
> > >>  >>  Benoît Roesslinger
> > >>  >>  Sent: Monday, June 30, 2008 10:46 AM
> > >>  >>  To: programming@...
> > >>  >>  Subject: [Jprogramming] Random number generation
> > >>  >>
> > >>  >>  Hi,
> > >>  >>
> > >>  >>  I am new to J and when doing some
> experiments with random
> > >>  >>  number generation
> > >>  >>  I stumbled across the following behavior,
> which wasn't what
> > >>  >>  I'd expect :
> > >>  >>
> > >>  >>     f=: 3 : 'y * ?0'
> > >>  >>     f 3
> > >>  >>  2.91414
> > >>  >>     f 3
> > >>  >>  0.139888
> > >>  >>     f 3
> > >>  >>  0.990328
> > >>  >>
> > >>  >>  OK so far, but when I tried:
> > >>  >>
> > >>  >>     f 3 3
> > >>  >>
> > >>  >>  it gives me :
> > >>  >>
> > >>  >>  0.0403801 0.0403801 (same values!)
> > >>  >>
> > >>  >>  whereas I'd expect a behavior much like the
> one of '?'...
> > >>  >>  Is this behavior normal ?
> > >>  >>  Suppose I want to create a function to
> generate a random
> > >>  >>  deviate from a
> > >>  >>  distribution (normal for instance) with some
> parameters (mean
> > >>  >>  and sd for
> > >>  >>  instance) that will work in the same fashion
> as '?', ie
> > >>  it is
> > >>  >>  possible to
> > >>  >>  generate lots of random deviates at once
> using code
> > >>  such as :
> > >>  >>  distri 100 $
> > >>  >>  x, where x would represent parameters, what
> is the best
> > >>  way to go ?
> > >>  >>
> > >>  >>  Many thanks in advance!
> > >>  >>
> > >>  >>  Benoît.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Random number generation

by Joey K Tuttle :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, what I was attempting to state in my message
when I said, '(my) f2' which further down in the
message is defined as:

   f2 =: 3 : 'y * ?($y)$0'

to which your original response was correctly
directed (thank you again). As usual with "timing
discussions" - many angels dancing on the pin
head here....

- joey



At 08:15  -0700 2008/07/01, Roger Hui wrote:

>Timing is not everything.  In this case f2 is computing
>something weird and possibly misleading:  All the
>elements of a row are the same. e.g.
>
>    f2 3 4$9
>2.06843 2.06843 2.06843 2.06843
>5.20443 5.20443 5.20443 5.20443
>1.00311 1.00311 1.00311 1.00311
>    f3 3 4$9
>  3.36383 8.62184  6.76132 4.76392
>  8.01329 7.10008 0.285073 3.84514
>0.950537 6.16313 0.214141 3.56592
>
>
>
>----- Original Message -----
>From: "R.E. Boss" <r.e.boss@...>
>Date: Monday, June 30, 2008 23:56
>Subject: RE: [Jprogramming] Random number generation
>To: 'Programming forum' <programming@...>
>
>>     f3=: 3 : 'y * ($y) ?@$ 0'
>>     f2=: 3 : 'y * ?(#y)#0'
>>
>>     ts 'f2 Y' [Y=:1000 1000$9
>>  0.0098060407 8402944
>>     ts 'f3 Y'
>>  0.034617353 16781696
>>
>>
>>  R.E. Boss
>>
>>
>>  > -----Oorspronkelijk bericht-----
>>  > Van: programming-bounces@... [mailto:programming-
>>  > bounces@...] Namens Joey K Tuttle
>>  > Verzonden: maandag 30 juni 2008 19:41
>>  > Aan: Programming forum
>>  > Onderwerp: Re: [Jprogramming] Random number generation
>>  >
>>  > Interestingly, I see almost no change in speed/space between
>>  (my) f2 and
>>  > f3...
>>  >
>>  >     ts 'f2 1000 1000$9'
>>  > 0.231313 2.20224e7
>>  >     ts 'f3 1000 1000$9'
>>  > 0.235067 2.0974e7
>>  >
>>  > Well - I guess a million bytes in space saving is
>>  > nothing to sneeze at (used to be a lot of
>>  > memory...) so I applaud the optimization work you
>>  > have done!
>>  >
>>  > Thanks - joey
>>  >
>>  > At 10:08  -0700 2008/06/30, Roger Hui wrote:
>>  > >To gain more speed (and reduce space), use:
>>  > >
>>  > >f3=: 3 : 'y * ($y) ?@$ 0'
>>  > >
>>  > >?@$ is supported by special code.  The difference
>>  > >is most striking for random booleans:
>>  > >
>>  > >    ts=: 6!:2 , 7!:2@]
>>  > >
>>  > >    ts '1e6 ?@$ 2'
>>  > >0.00275063 1.04934e6
>>  > >    ts '?1e6$2'
>>  > >0.00555685 5.24352e6
>>  > >
>>  > >
>>  > >
>>  > >----- Original Message -----
>>  > >From: Joey K Tuttle <jkt@...>
>>  > >Date: Monday, June 30, 2008 8:54
>>  > >Subject: RE: [Jprogramming] Random number generation
>>  > >To: Programming forum <programming@...>
>>  > >Cc: 'Programming forum' <programming@...>
>>  > >
>>  > >>  Not to quibble... but
>>  > >>
>>  > >>      f2 =: 3 : 'y * ?(#y)#0'
>>  > >>
>>  > >>  has a related trouble to the original post with
>>  > >>  argument rank > 1 ... f1 is actually better in
>>  > >>  extending "to work with arrays of any shape". To
>>  > >>  try to gain some speed, perhaps you really meant
>>  > >>  to say:
>>  > >>
>  > > >>      f2 =: 3 : 'y * ?($y)$0'
>>  > >>
>>  > >>  - joey
>>  > >>
>>  > >>
>>  > >>  At 10:59  -0400 2008/06/30, Henry Rich wrote:
>>  > >>  >What you executed was:
>>  > >>  >
>>  > >>  >3 3 * ?0
>>  > >>  >
>>  > >>  >which is
>>  > >>  >
>>  > >>  >3 3 * (?0)
>>  > >>  >
>>  > >>  >in other words, you asked for one number, then
>>  multiplied it by
>>  > >>  3 twice.
>>  > >>  >
>>  > >>  >
>>  > >>  >You could have your verb apply to atoms by giving it
>>  a rank of 0:
>>  > >>  >
>>  > >>  >    f1 =: 3 : 'y * ?0'"0
>>  > >>  >
>>  > >>  >Or, you could design it to work with arrays of any shape:
>>  > >>  >
>>  > >>  >    f2 =: 3 : 'y * ?(#y)#0'
>>  > >>  >
>>  > >>  >Working with bigger arrays is faster, but perhaps
>>  not by enough
>>  > >>  >to be worth the trouble.  Depends on your
>>  application.> >>  >
>>  > >>  >Henry Rich
>>  > >>  >
>>  > >>  >
>>  > >>  >>  -----Original Message-----
>>  > >>  >>  From: programming-bounces@...
>>  > >>  >>  [mailto:programming-bounces@...]
>>  On Behalf Of
>>  > >>  >>  Benoît Roesslinger
>>  > >>  >>  Sent: Monday, June 30, 2008 10:46 AM
>>  > >>  >>  To: programming@...
>>  > >>  >>  Subject: [Jprogramming] Random number generation
>  > > >>  >>
>>  > >>  >>  Hi,
>>  > >>  >>
>>  > >>  >>  I am new to J and when doing some
>>  experiments with random
>>  > >>  >>  number generation
>>  > >>  >>  I stumbled across the following behavior,
>>  which wasn't what
>>  > >>  >>  I'd expect :
>>  > >>  >>
>>  > >>  >>     f=: 3 : 'y * ?0'
>>  > >>  >>     f 3
>>  > >>  >>  2.91414
>>  > >>  >>     f 3
>>  > >>  >>  0.139888
>>  > >>  >>     f 3
>>  > >>  >>  0.990328
>>  > >>  >>
>>  > >>  >>  OK so far, but when I tried:
>>  > >>  >>
>>  > >>  >>     f 3 3
>>  > >>  >>
>>  > >>  >>  it gives me :
>>  > >>  >>
>>  > >>  >>  0.0403801 0.0403801 (same values!)
>>  > >>  >>
>>  > >>  >>  whereas I'd expect a behavior much like the
>>  one of '?'...
>>  > >>  >>  Is this behavior normal ?
>>  > >>  >>  Suppose I want to create a function to
>>  generate a random
>>  > >>  >>  deviate from a
>>  > >>  >>  distribution (normal for instance) with some
>>  parameters (mean
>>  > >>  >>  and sd for
>>  > >>  >>  instance) that will work in the same fashion
>>  as '?', ie
>>  > >>  it is
>>  > >>  >>  possible to
>>  > >>  >>  generate lots of random deviates at once
>>  using code
>>  > >>  such as :
>>  > >>  >>  distri 100 $
>>  > >>  >>  x, where x would represent parameters, what
>>  is the best
>>  > >>  way to go ?
>>  > >>  >>
>>  > >>  >>  Many thanks in advance!
>>  > >>  >>
>>  > >>  >>  Benoît.
>----------------------------------------------------------------------
>For information about J forums see http://www.jsoftware.com/forums.htm

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