Re: Random Distribution in Forth
- From: jrh <no@xxxxxxxx>
- Date: Thu, 28 Jun 2007 07:45:52 GMT
mhx@xxxxxx (Marcel Hendrix) wrote in news:01003428173560@xxxxxxxxxxxxxx:
jrh <no@xxxxxxxx> writes Re: Random Distribution in Forth<clip>
1 EQU MASK
: RAND1+/- ( n -- n+1/-1 ) RAND MASK AND IF 1- ELSE 1+ THEN ;
Guessing that 'EQU' is CONSTANT
A literal constant.
MASK could be any power of two depending on which bit is being tested.
and RAND a random number generator,
Yes, RANDOM might be the word in ANSI Forth.
I assume:
: RAND1+/- ( n -- n+1/-1 )
RAND MASK AND \ get a single random bit
IF 1- \ bit = '1' : increment n
ELSE 1+ \ bit = '0' : decrement n
THEN ;
You have chosen to convert the lowest bit of the random number
generator to '1' / '-1'. Most PRNG have bad lower bits. Is MASK
set to 1 on purpose?
The actual program tests all 64 bits, the counters for
some bits never get back to zero, even after millions of cycles.
: TEST-RAND { n - t - t }
0 -> t
This is unfamiliar locals syntax (for me).Our locals were implimented before the standard and work a little
You mean 0 { n t -- u } ?
different, n is defined from the stack , t is undefined and must
be initialized.
0 n 0
?DO RAND1+/- DUP 0=
IF t 1+ -> t THEN
LOOP t
;
So you randomly add +1 or -1 to 0, and do
this n times. The number of times the running sum becomes
0 is counted (in t).
Yes, and if RAND was producing and equal number of ones and zeros
you would think the counter should keep crossing zero, not
drift off in a positive or negative direction.
\ If RAND is linear
\ What is the function of n for the average value of t
Linear? You mean uniform?
Yes, all numbers have the same chance of being generated.
All for now,
jrh
.
- References:
- Random Distribution in Forth
- From: jrh
- Re: Random Distribution in Forth
- From: Marcel Hendrix
- Random Distribution in Forth
- Prev by Date: Re: Build your own Forth for Microchip PIC: Design thoughts
- Next by Date: Re: Why aren't the forth people doing this?
- Previous by thread: Re: Random Distribution in Forth
- Next by thread: Re: Random Distribution in Forth
- Index(es):
Relevant Pages
|