Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Random number generation IP Sharp APL?

8 views
Skip to first unread message

Fredric L. Rice

unread,
Jun 23, 2006, 10:11:59 PM6/23/06
to
How can I generate random numbers with Sharp's APL?

The utility workspace that comes with the IBM PC package uses
the []ts timestamp system variable however it doesn't do a good
enough job since I need to generate thousands of random numbers
a second.

---
Pretending that you don't smell the burning Jews isn't an excuse
for the defense of war crime atrocities against humanity. "I
didn't know" is no longer an excuse in the age of the Internet.

Tracker

unread,
Jun 24, 2006, 1:17:54 AM6/24/06
to

Jimserac

unread,
Jun 24, 2006, 8:42:30 AM6/24/06
to

Random numbers are one of my interests so I clicked on the link
but got mish mosh instead of code. I then tried the "Click Here"
link and checked that I already have each of the fonts
listed. I reinstalled all of them just to make sure. I then
tried every APL font that I had including APL Net, APL Help, APL
Plus... etc.

I tried the firefox, latest Opera and IE browsers.

None worked.

Could someone please tell me how to view the code.

I must be stupid, not for being able to display the code but for having
the patience to waste time with this instead of going directly to J.

In fact, the hell with it. I've already started going to J so why
bother with this nonsense.

Jim

Tracker

unread,
Jun 24, 2006, 9:51:09 AM6/24/06
to

First, you need an APL Font which it sounds like you have. The link on the above page to fonts is: http://www.sigapl.org/font.htm

Second, in Firefox, you can change the page encoding from the default "Unicode (UTF-8)" to "Western (ISO 8859-1)" or "Western (Windows-252)".

AA2e72E

unread,
Jun 24, 2006, 10:00:50 AM6/24/06
to
Jimserac wrote:

"In fact, the hell with it. I've already started going to J so why
bother with this nonsense."

The page is using the APLPLUS font. The problem is the browser rather
than the APL font.

If you have a pain in the head, it is wiser to try headache pils before
considering amputation ...

I'll try to send you a PDF of the pages.

Jimserac

unread,
Jun 24, 2006, 12:52:32 PM6/24/06
to

Thanks for the help everyone.

Nah, I'm going keep my head and amputate
the language!!!!

Thanks again,
Jim

AA2e72E

unread,
Jun 24, 2006, 2:35:44 PM6/24/06
to
Just in case Lycos does not deliver, Jimserac & Markus, I have sent the
PDF; if you do not receive it, let me know.

Fredric L. Rice

unread,
Jun 24, 2006, 6:01:45 PM6/24/06
to
Tracker <nu...@email.net> wrote:

>See: http://www.sigapl.org/random.htm

I get garbage. I'll try to come up with something on my own
but thanks for the attempt. }:-}

>Fredric L. Rice wrote:
>> How can I generate random numbers with Sharp's APL?

---

AA2e72E

unread,
Jun 24, 2006, 6:35:27 PM6/24/06
to

Jimserac wrote:
> Nah, I'm going keep my head and amputate the language!!!!

Good luck. I hope you have a sustainable appetite for alphabet soup!

Jimserac

unread,
Jun 24, 2006, 6:37:45 PM6/24/06
to

Tracker wrote:

> First, you need an APL Font which it sounds like you have. The link on the above page to fonts is: http://www.sigapl.org/font.htm
>
> Second, in Firefox, you can change the page encoding from the default "Unicode (UTF-8)" to "Western (ISO 8859-1)" or "Western (Windows-252)".

ISO 8859-1 worked immediately for me, thanks.

In the interests of driving away as many people
as possible from APL, it is to be hoped that the
managers of the SIGAPL web site continue
to leave out this little tidbit from their "click here"
explanation.

Thanks again,
Jim

Fredric L. Rice

unread,
Jun 24, 2006, 10:40:45 PM6/24/06
to
"Jimserac" <Jims...@gmail.com> wrote:

>Tracker wrote:

>> First, you need an APL Font which it sounds like you have. The link on the above page to fonts is: http://www.sigapl.org/font.htm
>> Second, in Firefox, you can change the page encoding from the default "Unicode (UTF-8)" to "Western (ISO 8859-1)" or "Western (Windows-252)".
>ISO 8859-1 worked immediately for me, thanks.

I think I'll try to get it working since I really could use it.

Ibeam2000

unread,
Jun 29, 2006, 3:32:30 PM6/29/06
to
What you need to do is find something which varies wildly between
executions. Often, it would be the milliseconds on []TS, but often you
can't even get that, depending on the granularity of the system's
clock. Set []RL to this number, and you should have more random random
numbers.

Fredric L. Rice

unread,
Jun 29, 2006, 8:49:15 PM6/29/06
to
"Ibeam2000" <Ibea...@gmail.com> wrote:

Thanks, I've got something like that working well now though it's
a bit more complicated because I have to generate thousands of
pseudo-random numbers as quickly as possible. I've got something
_reasonably_ working. }:-}

James J. Weinkam

unread,
Jun 29, 2006, 11:17:16 PM6/29/06
to
This is exactly the wrong thing to do.

The built in random number generator in every APL implementation that I have
seen uses what is technically known as a linear congruential random number
generator to generate a sequence of random integers. These are then
transformed into whatever form is needed for the purpose at hand.

In general, in a linear congruential sequence, the next member of the sequence
is computed from the current member by the formula:

next {is} modulus | constant + multiplier {times} current

The initial value of current is called the seed. APL calls it the random link
{quad}RL.

The usual practice is to choose the modulus, constant, and multiplier so that
the resulting sequence(s) simultaneously have a long period and exhibit good
randomness properties. Usually this is done by choosing the modulus first,
then deciding whether or not to use a non zero constant, then picking from
among all multiplier (and constants if non zero) that yield the maximum
possible period, specific value(s) which pass a number of tests for
randomness. For most systems that have some sort of built in random number
generator all of this is hard wired into the system. The only control the user
has over the process is to choose the initial value (seed).

Exactly how the seed should be chosen and the consequences of particular
choices depend on the modulus and constant.

If the constant is non zero then the maximum achievable period is equal to the
modulus. I.e., all values between 0 and modulus-1 will occur in some order
before the sequence repeats. This maximum period can only be achieved under
the following conditions:
1. The constant and modulus must be relatively prime (i.e., have no common
factor other than 1).
2. Every prime divisor of the modulus must also be a divisor of multiplier-1
3. If 4 is a divisor of the modulus it must also be a divisor of multiplier-1

In this situation, all choosing the seed does is choose the starting point in
the one and only permutation of the integers 0...modulus-1 that the generator
produces.

If the constant is zero the maximum achievable period depends on the prime
factorization of the modulus and can be achieved under the following
conditions:
1. The seed and the modulus are relatively prime
2. The multiplier is a primitive modulo the modulus

In this situation, if the modulus is a prime, p, then the maximum period is p-1
and the seed, which can be any value between 1 and p-1 inclusive again simply
chooses the starting point in the one and only permutation of the integers
1...p-1 that the generator produces. On the other hand, if the modulus is
composite, then the choosing the seed selects not only the starting point but
also which of several disjoint sequences the generator produces. Moreover
choosing a seed which is nor relatively prime to the modulus causes the
generator to produce a subsequence which is not of maximum length.

From this you can see that unless you know quite a bit about the specific
random number generator you are using you have no business messing with the
seed at all. You could easily shoot yourself in the foot.

Why would one set the seed?

One reason is to ensure that exactly the same sequence of values is used on
separate occasions. This is useful in debugging.

A second reason is to ensure that several replications of an experiment are
done using independent sequences of random numbers. Now here is where picking
seeds out of the air is fraught with danger. You may well choose a seed for
the second experiment that simply gives the same sequence as was used in the
first experiment shifted by a few places. This will in general lead to a high
correlation between the two results instead of the desired independence. The
easiest way to avoid this pitfall is to simply continue using random numbers
from the same sequence without reseeding the generator for each replication.
If the replications must be done in separate runs, this can be achieved by
saving the value of the seed at the end or each run and using that value to
seed the generator at the start of the next run.

In some applications, in order to reduce the variance of the results, one wants
to have separate random number generators for various separate needs for random
values within the application. Say, for example, that you need four separate
random number streams. You achieve this by picking four seeds that are far
enough apart in the sequence produced by the underlying linear congruential
generator that the four sub sequences do not overlap. In APL terms, you would
then set and save {quad}RL before and after each use of {roll} or {deal}.
Obviously you can only do this if you come armed with a knowledge of the
parameters of the underlying linear congruential generator.

For the OS/2 version of APL2, a little experimentation reveals that

constant=0
modulus=2147483647, which is (2*31)-1, which is prime
multiplier=16807
initial {quad}RL=16807

I have no idea what values are used in other implementations of APL but it isn't
hard to devise experiments to discover what values are being used.

Fredric L. Rice

unread,
Jun 30, 2006, 9:31:06 PM6/30/06
to
"James J. Weinkam" <j...@cs.sfu.ca> wrote:

>The built in random number generator in every APL implementation that I have
>seen uses what is technically known as a linear congruential random number
>generator to generate a sequence of random integers. These are then
>transformed into whatever form is needed for the purpose at hand.

...cuts...

I've put your note to paper so I can see if there's details I can
use to improve what I'm doing. SHARP's APL comes with a utility
workspace that contains a function called randomize that SHARP says
is good for reseeding []rl but I'm not happy with it because I
find that frequent, very fast ? statements there are repeats that
have a frequency that I feel aren't "random" enough.

In my StarTrek game the placement of klingons, stars, bases, and
planets became "stringy" in that plotted to the screen one could
detect non-randomness in the random numbers prodoced by ?

Richqw

unread,
Jun 30, 2006, 10:51:41 PM6/30/06
to
Thanks for an excellent discussion of random number generators.

Richard Wendt

James J. Weinkam

unread,
Jul 1, 2006, 12:31:21 AM7/1/06
to
Fredric L. Rice wrote:
> "James J. Weinkam" <j...@cs.sfu.ca> wrote:
>
>
>>The built in random number generator in every APL implementation that I have
>>seen uses what is technically known as a linear congruential random number
>>generator to generate a sequence of random integers. These are then
>>transformed into whatever form is needed for the purpose at hand.
>
>
> ...cuts...
>
> I've put your note to paper so I can see if there's details I can
> use to improve what I'm doing. SHARP's APL comes with a utility
> workspace that contains a function called randomize that SHARP says
> is good for reseeding []rl but I'm not happy with it because I
> find that frequent, very fast ? statements there are repeats that
> have a frequency that I feel aren't "random" enough.
>
Unfortunately, your, or anyone else's, feelings are a notoriously poor judge of
randomness. This fact underlies the birthday "paradox".

The only sound method of testing whether a sequence of ? statements is "random
enough" is to do a series of experiments in which ? is used repeatedly in a
nontrivial context where the probability distribution of the outcomes is known
and then apply an appropriate statistical test to the series of results. To be
really valid, numerous replications of the test must be performed and several
distinctly different experments should be tested in this way.

Sadly, most people use the method used by Don Quixote in testing his helmet:
After smiting it with his sword and destroying it, he rebuilt it using the same
method he had originally used and decided it had no further need of testing.

On the other hand, there may be some basis for your feelings, since the built in
generator is not particularly good. The popularity of the single linear
congruential generator is based more on its simplicity of implementation than
its quality. However it must be borne in mind that the types of applications
for which knowledgeable programmers are content to use a built in generator are
not particularly demanding.

rbe

unread,
Jul 24, 2006, 12:50:38 PM7/24/06
to
See below.


1. Weinkam is correct that "...the built-in generator is not
particularly good."
At the time that APL\360 and its various offspring came into being
early 1960's),
it was, I believe, considered good, but there are much better
generators available now. Knuth Vol III, I think, contains a dated, but
readable, introduction to
random number generators.

2. Here is the lehmer function that lies at the core of the APL\360,
APL2, SHARP APL random number generators:

r{<-}lehmer
@ Lehmer's so-so random-number generator.
@ CACM 1966-06, p. 432
@ Robert Bernecky 2005-08-12
r{<-}QUADrl{<-}({neg}1+2*31)|16807{times}QUADrl

3. This function is used as a sub-function by roll and deal.
Here's roll for a scalar argument:

r{<-}roll y
@ Monadic query: roll
@ Uses global QUADrl
@ Robert Bernecky 2005-08-12
{->}(1{epsilon}y{<=}0){rho}.1 @ Argument must be postive
r{<-}lehmer
r{<-}#io+{floor}(r{times}y){divide}{neg}1+2*31

You should get the same results from:

QUADrl {<-} {quad}rl
roll each 10
and
? 10

QUADrl and {quad}rl should also have same value afterwards.


4. I don't have a copy of deal handy, as it's a bit messier
(particularly
in the old days, when workspace sizes (32k was default!) made
set membership and friends harder. Hmm. That was also before I came up
with fast set membership and indexof algorithms for APL...

Bob

0 new messages