HP Forums

Full Version: Uniform Random Number Algorithm
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I don't know if this has been discussed before, but my new prime and old HP71B use the same algorithm for generating random numbers. If you set the seed to 1 (RANDOMIZE(1) for the 71B) you get the following series:
.73136...
.77202...
.989727...
.248998...

and so on.
Yup. The math library was re-implemented in portable C code from the older Saturn assembly/sysrpl back for the 20/30b calculators and has been used moving forward.
Thanks.

I am busy programming my goto first program on all my new languages. It uses random numbers to calculate pi. I first used it on an IBM 360 in the 70's. (Sorry, no speed bench mark for that one.)
Would be great if HP Prime adopted a better RNG, like Mersenne Twister. Machine is so modern and capable, but not for real simulations.
(01-22-2017 02:33 AM)mark4flies Wrote: [ -> ]Would be great if HP Prime adopted a better RNG, like Mersenne Twister. Machine is so modern and capable, but not for real simulations.

The CAS does use better generators...
So what CAS command would get me a uniform random number from 0 to 1? I can find random polynomials and random vectors and even random numbers from various distributions, but no random numbers.
rand() -- lowercase
Maybe that should be in the manual. 8^)

ETA:
It does not show up in the on-calculator help, either.
For more info you can check the (prime related) giac/xcas help system.
It is specifically NOT in the manual by design to keep things simple. Same reason why it doesn't appear in the catalog - only power users highly interested in such things will ever have a need for anything other then a "more then good enough" random generator.

Having multiple functions that do (to the normal, non-power user) identical things is not really a good idea in general. We'd then have to explain why all the random number generation seed value setting is different for different functions, why one is slower then the other, why different generators are used, etc.

This prevents people who might get confused from stumbling across it, not matching results that others are getting, and so on - while allowing users like yourself to access it.

The alternative would be just to remove it completely. Undecided
Actually the Prime CAS does not use the Mersenne Twister, while giac/xcas does, more precisely tinymt32.c which has the following copyright header
Code:
/**
 * @file tinymt32.c
 *
 * @brief Tiny Mersenne Twister only 127 bit internal state
 *
 * @author Mutsuo Saito (Hiroshima University)
 * @author Makoto Matsumoto (The University of Tokyo)
 *
 * Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
 * Hiroshima University and The University of Tokyo.
 * All rights reserved.
 *
 * The 3-clause BSD License is applied to this software, see
 * LICENSE.txt
 */
The Prime CAS is using a congruential generator:
Code:
 r = unsigned ((1664525*ulonglong(r)+1013904223)%(ulonglong(1)<<31));
Regarding the "secret" rand function, I think it's the only built-in random function that can be used to generate a list of random integers without repetition. This is also referred to as "selection without replacement".

Example: rand(10,0,9) --> all ten single-digit integers, without any repeats, randomly shuffled.

RANDINT doesn't do this; it allows repeats.

N.B. If you want to use rand(a,b,c) in Home, then the "Change apparent integers into exact integers" setting (CAS Settings, page 1, 3rd line, right end) must be checked.
Looks like "rand" is wrongly named - It should be "randperm".
Except if you call it with no arguments you just get a single random number between 0 and 1 with a uniform distribution.
(01-23-2017 06:33 PM)Tim Wessman Wrote: [ -> ]It is specifically NOT in the manual by design to keep things simple. Same reason why it doesn't appear in the catalog - only power users highly interested in such things will ever have a need for anything other then a "more then good enough" random generator.

Having multiple functions that do (to the normal, non-power user) identical things is not really a good idea in general. We'd then have to explain why all the random number generation seed value setting is different for different functions, why one is slower then the other, why different generators are used, etc.

This prevents people who might get confused from stumbling across it, not matching results that others are getting, and so on - while allowing users like yourself to access it.

The alternative would be just to remove it completely. Undecided

How about an Advanced Users Guide for power users that includes more information than the standard manual does? The argument that hiding information will make something simpler to use seems specious to me.

Tom L
(01-23-2017 06:56 PM)parisse Wrote: [ -> ]Actually the Prime CAS does not use the Mersenne Twister, while giac/xcas does, more precisely tinymt32.c which has the following copyright header
Code:
/**
 * @file tinymt32.c
 *
 * @brief Tiny Mersenne Twister only 127 bit internal state
 *
 * @author Mutsuo Saito (Hiroshima University)
 * @author Makoto Matsumoto (The University of Tokyo)
 *
 * Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto,
 * Hiroshima University and The University of Tokyo.
 * All rights reserved.
 *
 * The 3-clause BSD License is applied to this software, see
 * LICENSE.txt
 */
The Prime CAS is using a congruential generator:
Code:
 r = unsigned ((1664525*ulonglong(r)+1013904223)%(ulonglong(1)<<31));

Not pushing! But if the better RNG is already incorporated, why not be consistent and uniform. Just asking. Monte Carlo methods are more important every day, so best RNG is vital. Thanks!
It's not a technical issue, it's a legal issue.
Reference URL's