Operating System - OpenVMS
1751802 Members
4952 Online
108781 Solutions
New Discussion юеВ

Re: Generating Random Numbers.

 
SOLVED
Go to solution
The Brit
Honored Contributor

Generating Random Numbers.

Is there any current system call that will generate (pseudo) random numbers. Something similar to the old MTH$RANDOM that used to exist in the old days, OpenVMS 6.1(?) and earlier.

Dave.
12 REPLIES 12
Hoff
Honored Contributor

Re: Generating Random Numbers.

Fortran has RAN and RANDU, Pascal has RANDOM(), BASIC has RND, and C has probably the best of the random functions with random() and friends.

Based on a quick Google, the core of MTH$RANDOM call is akin to SEED = (69069*SEED + 1) mod 2**32, and X = SEED/2**32, RETURN X. It's all of a handful of instructions, at most.

There's $hash_password

The OpenSSL stuff has a random function.

There are the message digest calls, too.

Hein van den Heuvel
Honored Contributor

Re: Generating Random Numbers.


Hi Dave,

What makes you think MTH$RANDOM went away?
The HELP may be gone, but the code is there just fine. Has to be... compatibility.

Any particular language you'd like to use it from? For example, C has its own RAND.

The example below, from the Cobol Users still works fine on 8.3.

Also two old wizard articles:
http://h71000.www7.hp.com/wizard/march-96/question_2.html
http://h71000.www7.hp.com/wizard/wiz_9817.htm

Hope this helps some,

Hein


http://h71000.www7.hp.com/doc/82final/6297/6297pro_100.html

IDENTIFICATION DIVISION.
PROGRAM-ID. RUNTIME.

*****************************************************
* This program calls MTH$RANDOM, a random number *
* generator from the Run-Time Library. *
*****************************************************
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEED PIC 9(5) COMP VALUE 967.
01 A-NUM COMP-1.
01 C-NUM PIC Z(5).
PROCEDURE DIVISION.
GET-RANDOM-NO.
PERFORM 10 TIMES
CALL "MTH$RANDOM" USING SEED GIVING A-NUM
MULTIPLY A-NUM BY 100 GIVING C-NUM
DISPLAY "Random Number is " C-NUM
END-PERFORM.
The Brit
Honored Contributor

Re: Generating Random Numbers.

I was approached by one of our developers. The language in question is DIBOL. (OpenVMS version is 8.3-1H1)

Hoff,
I pointed him to the C RTL, and gave him a link to the documentation (for OpenVMS 8.3).

I was just wondering if there was a system routine he could call directly.

Dave
abrsvc
Respected Contributor

Re: Generating Random Numbers.

Please also realize that this random function is poor at best. Depending upon how it is used, it may not really be anywhere close to random. While you may not need a more elaborate routine, there are ones that exist that are easy to code and are significantly more random over the value range than the "standard". Don Knuth has an entire volume almost dedicated to this topic.

Let us know if you need a better generator.

Dan
Joseph Huber_1
Honored Contributor

Re: Generating Random Numbers.

For better random generators, and implemented in many systems and languages, read about the "Mersenne twister" and its alternatives at
http://en.wikipedia.org/wiki/Mersenne_twister
http://www.mpp.mpg.de/~huber
Robert Gezelter
Honored Contributor

Re: Generating Random Numbers.

Dave,

MTH$RANDOM is still there, insofar as I know. Whether it is suitable for the intended application is a far more subtle question.

My recollection (I do not have the manuals with me where I am sitting at this instant) is that MTH$RANDOM is a linear congruential generator. Having done some work over the years in the random number "playground", there are some purposes for which that is adequate, and there are some that it is not. A review of a good text is in order before considering this question. Knuth Volume 2 (Seminumerical Algorithms) is one of the classic references. Alternatively, consult someone who has familiarity with the issues.

One of the original widely distributed random number generators (as part of a Scientific Subroutine Package known as SSP), had coefficients which were poor choices. My understanding is that this did result in some problems with simulations (my recollection is that Knuth Volume 2 has the discussion, as do several other numerical methods text that cover random numbers).

There is a snippet of the the help text for MTH$RANDOM at:
http://h71000.www7.hp.com/wizard/march-96/question_2.html

- Bob Gezelter, http://www.rlgsc.com
John Gillings
Honored Contributor

Re: Generating Random Numbers.

MTH$RANDOM works just fine of pseudo random numbers.

However, be careful with the data type of the return value. I believe it's F_FLOAT. Make sure you deal with any necessary conversions if you're using IEEE floating point types.

I disagree with Dan's assertion "Please also realize that this random function is poor at best."

The choice of parameters for the algorithm is based on Knuth, and is about as good as you'll get for a 32 bit implementation of a mixed congruential generator.

If you want a "true" random number generator (as opposed to a deterministic one which will return the same sequence for a given seed), you can't do it using a deterministic system. You'll need some physical device which gets numbers from truly random events, such as radioactive decay.

A crucible of informative mistakes
Hoff
Honored Contributor
Solution

Re: Generating Random Numbers.

What might be applicable here depends on the task.

The sys$create_uid call can be useful for some tasks where folks have sought random numbers, as can be the f$unique DCL lexical function, for instance.

The simplest answer here may well be the right one.

Or not.
John Gillings
Honored Contributor

Re: Generating Random Numbers.

Just to make sure it's clear...

sys$create_uid and f$unique are essentially the same. F$UNIQUE just translates the UID into a hex string (or some permutation of it).

However, these numbers, as returned, are definitely NOT random. They fail immediately because they're guaranteed to be monotonic increasing, and they're highly correlated with the time.

For some purposes you could select a fast varying region of the returned string. This MAY be random enough for some purposes, but you're effectively randomising on the time period between calls. I doubt you'll find any simple algorithm that generate give plausible random numbers in a loop calling F$UNIQUE.
A crucible of informative mistakes