The implementation can be refined to avoid
proliferating the two verbs. Thus:
init=: 4 : 0
sn=. 'state',x
y2=. 2{.y,9!:42 '' NB. seed and optional RNG #
('parm',x)=: y2
9!:43 {:y2 NB. select RNG
9!:1 {.y2 NB. set seed
(sn)=: 9!:44 '' NB. record state
i.0 0
)
gen=: 4 : 0
sn=. 'state',x
9!:45 ".sn NB. set state
z=. [EMAIL PROTECTED]/y
(sn)=: 9!:44 '' NB. record state
z
)
x init seed,i initializes private independent
RNG x ; i is optional and specifies the RNG #
(RNG algorithm). x gen m,n generates [EMAIL PROTECTED]
using private independent RNG x . Thus:
'a' init 16807
'b' init 314159
'a' gen 10 1000
590 147 158 729 522 732 355 5 955 673
'b' gen 10 1000
867 439 794 893 437 543 272 665 586 595
'a' gen 10 1000
858 350 740 105 669 546 334 840 546 184
'b' gen 10 1000
103 809 553 195 69 605 785 339 937 456
'a' init 16807
'a' gen 10 1000
590 147 158 729 522 732 355 5 955 673
'a' gen 10 1000
858 350 740 105 669 546 334 840 546 184
'b' init 314159
'b' gen 10 1000
867 439 794 893 437 543 272 665 586 595
'b' gen 10 1000
103 809 553 195 69 605 785 339 937 456
----- Original Message -----
From: Roger Hui <[EMAIL PROTECTED]>
Date: Saturday, July 15, 2006 1:03 am
Subject: Re: [Jprogramming] private random number generator
> Please see the J6.01 dictionary page for ?
> http://www.jsoftware.com/books/help/dictionary/d640.htm
>
> In particular, since 9!:44 captures and 9!:45 sets
> the RNG state, and the state completely determines
> the generated numbers, it is straightforward to
> implement independent versions of ? . For example:
>
> inita=: 3 : 0
> 9!:1 seeda=: y
> statea=: 9!:44 ''
> i. 0 0
> )
>
> rolla=: 4 : 0
> 9!:45 statea
> z=. x [EMAIL PROTECTED] y
> statea=: 9!:44 ''
> z
> )
>
> initb=: 3 : 0
> 9!:1 seedb=: y
> stateb=: 9!:44 ''
> i. 0 0
> )
>
> rollb=: 4 : 0
> 9!:45 stateb
> z=. x [EMAIL PROTECTED] y
> stateb=: 9!:44 ''
> z
> )
>
> The following results demonstrate that rolla and
> rollb really are independent. The first set have
> them interleaved:
>
> inita 16807
> initb 314159
> 10 rolla 1000
> 590 147 158 729 522 732 355 5 955 673
> 10 rollb 1000
> 867 439 794 893 437 543 272 665 586 595
> 10 rolla 1000
> 858 350 740 105 669 546 334 840 546 184
> 10 rollb 1000
> 103 809 553 195 69 605 785 339 937 456
>
> Non-interleaved execution gives the same generated
> numbers:
>
> inita 16807
> 10 rolla 1000
> 590 147 158 729 522 732 355 5 955 673
> 10 rolla 1000
> 858 350 740 105 669 546 334 840 546 184
>
> initb 314159
> 10 rollb 1000
> 867 439 794 893 437 543 272 665 586 595
> 10 rollb 1000
> 103 809 553 195 69 605 785 339 937 456
>
> p.s. I can imagine that for some purposes the seed
> (a single 31-bit integer) may not be an adequate
> starting point. In that case you can tweak the
> state directly.
>
>
>
> ----- Original Message -----
> From: bill lam <[EMAIL PROTECTED]>
> Date: Friday, July 14, 2006 9:56 pm
> Subject: [Jprogramming] private random number generator
>
> > Suppose there are 2 routines, each will initialize the seed and
> > call ?
> > alternatively, because state of rng is global, what a routine
> will
> > get depend on
> > the other routine or the cache left. There is a possibility that
> > the sequence
> > got by a routine does not satisfy original statistical property
> of
> > randomness.
> > suppose rng give 0 1 0 1 0 1 0 1 0 1 ..... for calling ?2 repeatedly
> > then routine a get 0 0 0 0 0 ...
> > while routine b get 1 1 1 1 1 ...
> > originally it should be bi-modal and average 0.5, of course any
> > respectable rng
> > will not generate this sequence, but it might illustrate the
> problem.>
> > So, is it possible to make 2 independent instances of ? for
> > private use?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm