Re: ERROR IN AFONSO CODE



John Smith <jsmith_100@xxxxxxxxxxx> writes:

Another funny thing about the Afonso code,
since the PRINT statements are inside the v loop, and
v increments from 1 to 40000, there should be 40000 prints. Yet Afonso's output has only a few prints.

Hmmmm..

REM "TWO
CLS
pi = 4 * ATN(1)
nn = 32
all = 40000
vc = 1.96
RANDOMIZE TIMER
FOR v = 1 TO all
sx = 0: sy = 0
FOR j = 1 TO nn
a = SQR(-2 * LOG(RND)): r = RND
x = 100 + 4 * a * COS(2 * pi * r)
y = 100 + 4 * a * SIN(2 * pi * r)
sx = sx + x / nn: sy = sy + y / nn
NEXT j
t = ABS(sx - sy)
IF t <= vc THEN inn = inn + 1
IF t > vc THEN outt = outt + 1
LOCATE 10, 10
PRINT USING "######"; all - v
PRINT USING "##.###"; inn / v
PRINT USING "##.###"; outt / v
RANDOMIZE TIMER
NEXT v : END

Notice the "LOCATE 10, 10" statement in the code. I believe this
causes the next PRINT statement to display at row 10, column 10 on the
screen. So the three PRINT statements do an on-screen update,
replacing the values that were already there.

I'm surprised you didn't say anything about the use of "RANDOMIZE
TIMER" inside the v loop. This initializes the random number
generator using the timer as a seed. The timer has a fixed resolution
(1/100th of a second?) so if the computer is fast enough, the same
random numbers will be generated in more than one pass through the v
loop. Even if the computer is not fast enough, the timer values will
be close to each other and the first random numbers generated on
successive passes will be correlated with each other. Another thing
is that the RANDOMIZE statement in Microsoft BASIC only uses 16 bits
of the seed values, so there are only 65536 possible sets of random
numbers.

The code would be much better off with the "RANDOMIZE TIMER" in the v
loop removed. Then you could get the full benefit(?) of Microsoft's
24-bit linear congruential generator.

Scott
--
Scott Hemphill hemphill@xxxxxxxxxxxxxxxxxx
"This isn't flying. This is falling, with style." -- Buzz Lightyear
.



Relevant Pages

  • Re: ERROR IN AFONSO CODE
    ...  So the three PRINT statements do an on-screen update, ... TIMER" inside the v loop. ... 24-bit linear congruential generator. ...
    (sci.stat.math)
  • Re: Adding functions to generic package
    ... >> What do Randomize and Move do? ... How is that different from Splice? ... search for the iterator designating those nodes. ... > end loop; ...
    (comp.lang.ada)
  • Re: while loop into database insert not working
    ... > and place key details into a database for viewing as statistics ... > trouble with the while loop (inside the foreach). ... In such cases I would place a few diagnostic print statements to make ...
    (comp.lang.perl.misc)
  • Why is array pushed in thread sub not set after join?
    ... Perl 5.10.0 on Cygwin 1.5.25. ... the print statements seemed to show this was working fine, and it did appear to go faster, but after the loop I checked the size of the array whose contents had been pushed from each of those threads, and it was size 0. ...
    (comp.lang.perl.misc)
  • Re: question: what does this "fix" thingy here do? r=fix(rnd*6)+1
    ... > You don't want to be using GoTo, especially to jump out of a loop like ... One way to avoid it would be to use another array to store ... > dim i as long ... > randomize ' only do this once in the application ...
    (microsoft.public.vb.general.discussion)