HP Forums
Uniform Random Number Algorithm - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Uniform Random Number Algorithm (/thread-7629.html)



Uniform Random Number Algorithm - KeithB - 01-19-2017 06:09 PM

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.


RE: Uniform Random Number Algorithm - Tim Wessman - 01-19-2017 08:46 PM

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.


RE: Uniform Random Number Algorithm - KeithB - 01-19-2017 10:06 PM

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.)


RE: Uniform Random Number Algorithm - mark4flies - 01-22-2017 02:33 AM

Would be great if HP Prime adopted a better RNG, like Mersenne Twister. Machine is so modern and capable, but not for real simulations.


RE: Uniform Random Number Algorithm - Tim Wessman - 01-22-2017 06:12 PM

(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...


RE: Uniform Random Number Algorithm - KeithB - 01-23-2017 04:24 PM

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.


RE: Uniform Random Number Algorithm - Tim Wessman - 01-23-2017 05:21 PM

rand() -- lowercase


RE: Uniform Random Number Algorithm - KeithB - 01-23-2017 05:24 PM

Maybe that should be in the manual. 8^)

ETA:
It does not show up in the on-calculator help, either.


RE: Uniform Random Number Algorithm - DrD - 01-23-2017 06:02 PM

For more info you can check the (prime related) giac/xcas help system.


RE: Uniform Random Number Algorithm - Tim Wessman - 01-23-2017 06:33 PM

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


RE: Uniform Random Number Algorithm - parisse - 01-23-2017 06:56 PM

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));



RE: Uniform Random Number Algorithm - Joe Horn - 01-23-2017 07:25 PM

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.


RE: Uniform Random Number Algorithm - Gerald H - 01-25-2017 10:04 AM

Looks like "rand" is wrongly named - It should be "randperm".


RE: Uniform Random Number Algorithm - KeithB - 01-25-2017 03:41 PM

Except if you call it with no arguments you just get a single random number between 0 and 1 with a uniform distribution.


RE: Uniform Random Number Algorithm - toml_12953 - 01-25-2017 06:19 PM

(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


RE: Uniform Random Number Algorithm - mark4flies - 01-26-2017 01:26 AM

(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!


RE: Uniform Random Number Algorithm - parisse - 01-26-2017 07:49 AM

It's not a technical issue, it's a legal issue.