Showing posts with label PRNG. Show all posts
Showing posts with label PRNG. Show all posts

Saturday, January 4, 2025

Casio fx-CG 50: Pseudorandom Number Generator (PRNG) Stat Plot

Casio fx-CG 50: Pseudorandom Number Generator (PRNG) Stat Plot


Introduction


This program is an inspiration from a HHC 2024 talk given by Kuba Tatarkiewicz. Tatarkiewicz’s talk is about testing RNG (random number generators). You can see it here: https://www.youtube.com/watch?v=vSDfqCK-ENk



Premise of RANDGRPH: Generate a recursive sequence


r_n = frac( (A * r_n-1 + B) ^ C)


The program builds two lists and develops a scatter plot. I have the program set up to plot 60 points, but we can up to 999 points. The list starts with the initial point (0, seed). The program asks you whether to have the calculator provide the seed or you provide a seed (between 0 and 1).



Casio fx-CG 50 Code: RANDGRPH (252 bytes)


“RAN # GRAPH”

“(A×R+B)^C” ◢

“A”? → A

“B”? → B

“C”? → C

Menu “SEED?”, “RANDOM”, 1, “YOUR OWN”, 2

Lbl 1

Ran# → R

Goto 0

Lbl 2

“0≤R<1, SEED”? → R

Lbl 0

{0} → List 1

{R} → List 2

For 1 → I To 75

Augment(List 1, {R}) → List 1

Frac((A×R+B)^C) → R

Augment(List 2,{R}) → List 2

Next

S-Gph1 DrawOn, Scatter, List 1, List 2, 1, Dot, ColorLinkOff, Black, AxesOn

DrawStat



Examples


Example 1: r_n = frac(991 * r_n-1)


r_n = frac(991 * r_n-1)


Example 2: r_n = frac( (0.3 * r_n-1 + 1)^2 )


r_n = frac( (0.3 * r_n-1 + 1)^2 )


Example 3: r_n = frac( (r_n-1 + π)^5 )


r_n = frac( (r_n-1 + π)^5 )


Enjoy! Happy New Year, be safe, sane, strong, and take care. Forever grateful,


Eddie


All original content copyright, © 2011-2025. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Saturday, November 10, 2018

HP Prime: The Period of a Pseudo Random Number Generator

HP Prime:  The Period of a Pseudo Random Number Generator

Introduction

Most scientific calculators, mathematical programs, and spreadsheet programs will have random number generators that will generate numbers at random.  The most common generator is a one that produces numbers between 0 and 1.  Additional random functions will also allow the user to generate random integers and random numbers that fit the normal distribution.

It wasn't always this way.

In the 1970s and into the 1980s, scientific calculators and some computers did not have random number functions.  This facilitated a need for pseudo random number generators, hereafter noted as PRNG, which date back to the 1940s.  PRNGs used recursive functions to generate random numbers between 0 and 1.  Several examples include:

x_n+1 = frac(997 * x_n)

x_n+1 = frac(147 * x_n)

x_n+1 = frac( (π + x_n)^5 )

You select a seed, which the RPNG starts generating random numbers.  For example, using the PRNG of x_n+1 = frac(147 * x_n) with a seed of π/3 generates the random numbers:

0.938040026
0.891883822
0.106921834
0.717509598
0.473910906
0.664903182
and so on..



HP Prime Program RNGTEST

The program RNGTEST tests the period of a pseudo random number generator, given a seed. The period of a pseudo random number generator at a given seed is how many iterations it takes for the generator to return to it's initial (first) random number.  For example, if a generator's first random number is 0.1 and it takes 20 iterations for the generator to reach 0.1 again, then the period is 20.

The program sets an arbitrary maximum limit, set in the code of 50,000.  If the count reaches this limit, then the period of the generator is at least this maximum, possibly infinity.  You may change this limit as wished.  Thankfully the HP Prime's processor is fast, so the calculator can handle many interactions quickly.  Older calculators will require more time. 

The program:

rngf(x);  // random number generator

EXPORT RNGTEST(s)
BEGIN
// s = seed
// 2018-11-04 EWS
LOCAL i,k,x:=s;
// initial point
i:=rngf(x);
k:=0;
PRINT();
// loop
REPEAT 
x:=rngf(x);
PRINT(k+" , "+x);
k:=k+1;
UNTIL (x==i AND k>1) OR k>50000;
RETURN k-1;
END;

rngf(x)
BEGIN
RETURN FP(997*x);
END;

Note:  In the rngf(x) subroutine, you can insert any pseudo random number generator you want.

Testing a Simple PRNG

I had set up the program to test a simple PRNG x_n+1 = frac(997 * x_n). 

If the seed is picked with one decimal point (0.1 through 0.9), the period is only 4.  That means, the generator cycles only 4 numbers before repeating again, with the exception is 0.5, which period is only 1.  Specifically:

The seed 0.1 generates 0.1, 0.7, 0.9, 0.3, and then repeats.

The seed 0.2 generates 0.2, 0.4, 0.8, 0.6, and then repeats.

The seed 0.3 generates 0.3, 0.1, 0.7, 0.9, and then repeats.

The seed 0.4 generates 0.4, 0.8, 0.6, 0.2 and then repeats.

The seed 0.5 generates 0.5 and only 0.5.

You can see the repeating periods below. (click on the picture below)



For two decimal point seeds 0.01 to 0.99 (leaving out 0.10, 0.20, etc), from an random sample, you either get a period of 20, or if the seed ends in 5, a period of 4.

For three decimal points seeds 0.001 to 0.999 (leaving out 0.100, 0.110, etc), from a random sample, you either get a period of 100, or if the seed ends in 5, a period of 20.

For simple PRNGs, you are going to a seed with many decimal places to be able to generate different random numbers.

Source:

Shammas, Namir  "New Pseudo Random Number Generators:  Part 1" 2015.  http://www.namirshammas.com/NEW/prng1.pdf  Retrieved October 28, 2018.

Eddie

All original content copyright, © 2011-2018.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.  Please contact the author if you have questions.

Sunday, September 24, 2017

HHC 2017 In Review

HHC 2017 In Review

Hello Nashville, TN!


HHC 2017 took place on September 16 and 17, 2017 in Brentwood, TN.  If you have not gone to a HHC conference, and you love calculators and math, I strongly encourage you to attend.  The conferences take place typically around the latter half of September. 


Disclaimer

I will give a summary of each of the talks here, accompanied by a YouTube video produced by http://www.hpcalc.org/.  The hpcalc.org website is run by Eric Rechlin.  I am under a non-disclosure agreement, which means that I will not be able to discuss certain details of the conference due to confidentiality.

Programming Contests

There were two programming contests, which is presented during the conference, both for the conference attendees and the MoHPC forum.  For fun, please click on the links below.

Subject:  Happy Numbers

Subject: Egyptian Fractions


Day 1:  September 16, 2017

Jim Johnson:  HP 25-LP Panamatik’s New Woodstock Low-Power Upgrade Kit


The classic HP Woodstock Calculators (HP-21, PP-22, HP-25, HP-25C, HP-27, HP-29C) had rechargeable battery packs that also came with the AC plug in battery pack.  The ACT chip in the Woodstock calculators could get damaged if the AC plugged in without the battery or the faulty battery.  Panamatik created a new HP-25 LP kit to replace the ACT chip.  The ACT chip not only revives your Woodstock calculator but also adds features such as:

* 80 character alpha-numeric text.  When you are reviewing your programs, you will see the function name rather than the character code.
* The calculator will have a sleep mode.  This allows the calculator to last up to 10 days before recharging.
* A clock is added, which also adds the ability to set alarms.
* All memory becomes continuous (saved when shut off).

Gene Wright – Non-HP Desktop Scientific Calculators


During the height of scientific calculators in the 1970s, desktop versions of scientific calculators were manufactured.  Aside from the HP-9820, HP-9805A, HP-9815A, HP-9815S, and HP-9825A, Wright highlighted the follow scientific desktops:

* Compucorp 324G:  portable scientific calculator

* Rockwell 350:  8 digit calculator, algebraic

* Monroe 1920:  14 digit calculator, algebraic, linear regression.  Two keys, [ I ] and [ II ] are mapped to two switch selectors. 

The [ I ] key can either execute DMS>θ, r>θ (convert to degrees), xy>θr (rectangular to polar), π, Clr 1-3 (clear registers 1, 2, and 3), Σ (add data points). 

The [ II ] key can used for θ>DMS, θ>r (convert to radians), θr>xy (polar to rectangular), LOG (common logarithm, antilog is available via [INV] [ II ]), σ/mean (calculates standard deviation – stored in register 9 and mean – stored in register 8), -Σ (remove data points).
 
* Electronika MK-45:  features factorial and storage arithmetic, algebraic

* Sharp PC 1001: programmable calculator, 64 steps

* Victor VS 230:  extremely rare desktop scientific calculator

* Commodore 1540:  10 digits with 2 digit exponents, 2 memory registers

Gene Wright – New ROMs for the HP-41CL


An update on the HP 41CL, a replacement CPU board for the fullnut versions for the HP 41C.  The new version of the HP 41CL includes:

* Formula evaluation with an equation library
* Total Rekall:  adds additional function such as XEQ+ and LAST_F (last 5 operations).  There is an additional 5 registers available for emergency purposes.
* HP 67 Fun:  game programs ported from the HP 67.  Chess, Moon Lander, One Arm Bandit, and other games.
* Partial Differential Equations
* Differential Geometry
* Recursive FROOT (root) and FINTG (integrals)
* XROM rom
* Ladybug rom:  makes the 41C is an integer binary calculator
* Test Statistics Solution Rom
* PPC 9: PPC Statistics ROM

Sylvian Cote – 41CL Self-Update


Sylvian’s presentation is very detailed about the 41CL.  A 41CL version 5 is being currently developed with the target date to be released in late 2017-early 2018.  It is the going to be the last version produced.  Version 5 will have the RAM size of 1,024 KB, with flash size of 8 MB, and the number of flash pages will increase to 1,024, the largest of the 41CL. 

The update can be completed all at once or in parts. 


Daniel McDonald – The End Is Near or I Love Pessimism


McDonald addresses a very important question:  what is going to happen to our calculator collection when we die?  How can we ensure the knowledge we gained won’t be lost to future generations? 

Hopefully, when the planet finally switches to an all-app world, there will still be room for handheld calculators with keyboards, or at the very least, enjoy a renaissance (or several) like Model T cars, typewriters, and vinyl albums.

Bob Prosperi – Searching… for calculators


Want that latest calculator or trying to find the elusive calculator for your collection?  Look no further than the tips and tricks presented by Bob Propseri.  Search terms, wildcards, what phrases get the best results (be specific, most of the time), and the use of quotes and minus signs are discussed here. 

Sometimes, the information you are looking for is on a website that is no longer currently maintained.  A tool to try the Wayback Machine, https://archive.org/web/ where over 305 billion archived sites have been achieved.  Your mileage may vary.

On the next eBay or auction site search, if the description contains “I don’t know how to test”, Prosperi suggest just to skip that item because it is most likely broken.

Namir Shammas – PRNGs For Calculators


There is a method on how calculators and computers generate random numbers, typically between 0 and 1 on calculators. Calculators and computers use a PRNG, a pseudo-random number generator to generate random numbers.  While computers use integer-based methods, calculators use floating number-based methods.  The latter generates less “random” values but is forgiven because we call for less random numbers on calculators. 

Regarding integer-based methods, a large integer is generated, then it is divided by another large integer to arrive at the random number. 

Using Matlab, Shammas tested various PRNGs for the ability to generate random numbers.  Patricianly, a million random numbers were generated on each test.  The PRNGs were scored by a penalty factor. 

Some examples of PRNGs are:

* frac(( 1111 + (9997 -1111)*r) * r)
* frac(997 * r)
* frac(π + r)^3
* frac(111/(π + r))

Where frac is the fraction function and r is a seed number.  PRNGs can get really complex, some involve IF-THEN-ELSE routines while others involve more than one seed.

Eric Rechlin – HP Calculator Achieve Twentieth Anniversary


Rechlin tells the history of www.hpcalc.org, launched on August 21, 1997.  Rechlin also accounts about the beginnings of storing HP calculator programs on websites.  Congratulations on 20 years, Eric!

Definitely check out this site www.hpcalc.org.

Note:  Any of my HP Prime programs that I do on this blog is also available for download (author:  Eddie Shore).  Much appreciation and thanks Eric!

Gene Wright – Don’t Make Me Wait


Wright discusses the mathematical perils of waiting, particularly situations where there is a long line and only one server.  Wright discusses queueing theory and the weighing the costs of service (number of servers, which is seen) and the cost of waiting (unseen).  Calculations quickly get complex where more than one server is involved.

Gene Wright – Commodore Scientific Calculators


Did you know that Commodore (1954 – 1994) made computers and calculators?  They did.  Watch this video and learn about the Commodore 1489, SR-37, SR-4148, SR-7919, PR-50, and others.  My personal favorite is the M 55. 

The M 55 (1976) stands out because it offered the error function, Bessel functions, log gamma, matrix operations, and Leguerre polynomials.  The N-60 (1976) specialized in navigation.  The S-61 (1976) specialized in statistics.  The SR-4921 (1975) was Commodore’s RPN model.

A good place for older calculator manuals, including Commodore, is Katie Wesserman’s site http://www.wass.net/manuals/

Richard Nelson – Not So Simple Power Calculations


This talk covers Ohm’s Law, calculating effective power, and the problem with load power versus pot rotation.  The pot is referred to a potientmeter, an electrical contact that serves a voltage divider.  In the case that the potientmeter is used in a system of two terminals, then the potientmeter becomes a rheostat (resistance adjustor).  Nelson covers a subject that is not discussed in electronic engineering class.

End of Day 1 – we’ve been going from 8:00 AM to 9:00 PM. 

Day 2:  September 17, 2017

Richard Schwartz – Log Tables


Before handheld calculators (1970s) we had to use tables and slide rules to assist us in calculations.  Log tables were provided by the government to students which had calculated common logarithms (base 10) to four decimal places.  Schwartz discusses the rounding errors found in the tables and how they can be addressed.

Bob Prosperi – HP 75 Update


The HP 75 (1982 – 1986) is enjoying a Renaissance.  There were two versions of the HP 75:  the HP 75C (1982) and the HP 75D (1983 – 1986).  There are also several emulators of the HP 75.  The hardware is large but is a beauty to hold. 

Original specs of the HP 75:

* Programming Language:  BASIC
* CMOS Capricorn CPU coprocessor
* 16,000 byte memory plus 8,000 bytes module available
* AC Port with 3 ROM slots
* Works with the HP-IL and PIL-BOX

You can join a discussion group for the HP 75:  https://groups.io/g/hp75

Namir Shammas – Halley-Ostrowski Root-Seeking Method


Dedicated to Bill Zimmerly.

Methods are numerically finding the root of the equation f(x) = 0 include the well-known Bisection Method, Newton’s Method (the most famous and used), Halley’s Method, and Ostrowski’s Method.

Shammas talks about combing methods to get generate a method to get to the solution faster (less iterations) and more accurately.  Highlighted are the Halley-Ostrowski Method and the Ostrowski-Lagrange Method. 

Jack Schwartz – EduCalc: A Look Back


Schwartz talks and reminiscences about EduCalc, a well embraced store that operated in Laguna Niguel, CA from May 24, 1976 to December 31, 1997.  EduCalc was one of the places to go and contact for HP calculators and documentation. 

Fun fact:  Richard Nelson used to work for EduCalc, and he did voices message detailing events and news for EduCalc.  Go to the video in this section and go to the 50:36 to hear a sample.  Really neat.

If only I visited EduCalc when it was open before it closed its doors in 1997.  And I live near Laguna Niguel! 

Eddie Shore – Extending the HP-12C: Programming for Scientific Applications
This is my first time I talked at an HHC conference, where I covered how we can use the programming features of the HP-12C to take it beyond financial applications and to save as much space as possible.  I covered how to type π, absolute value, the modulus function, finding the number of digits in an integer, how to simulate subroutines, and other topics.


Documentation:

Another check mark off of my bucket list. 

Don Shepherd – Do Not Fold, Spindle, or Mutilate

Punch Cards

Punch cards had a long history, the first design in 1725, with automated processing available in the late 1800’s.  At first, punch cards stored vital data and statistics, until around the 1950s where punch cards became command programming statements. 

Although punch cards came in various shapes and sizes, the typical punch card had eight rows of the digits 0 through 9, with space on top.  The space on top held punches that dictate which characters were used, first the alphabet (A-Z), then numbers (0-9), finally assorted characters (. , $).

To run a program or store information, a lot of cards were used. 


A particular card was the Wang Calculator card, where each punch card had seventy nine rows of numbers in Octal base (0, 1, 2, 4, 10 (8 in decimal), 20 (16 in decimal), and 40 (32 in decimal)).

A particularly neat way to punch cards was to use the IBM Port-A-Punch.  The stylus was used to punch holes in the cards without mess. 

I didn't know that HP Manuals from the 1970s were pretty small and handheld.  Here is two of them for the HP 33E and early financial calculators.

 Goodies:  Radio Shack EC-4000 (TI-57), TI-35 Plus, Sharp EL-506G.  This conference always have excellent door prizes.


Can’t wait for HHC 2018!

Eddie


This blog is property of Edward Shore, 2017.

Python – Earth’s Radius and Gravity in US Units

Python – Earth’s Radius and Gravity in US Units Introduction The following script, gravus2.py, estimates the Earth’s gravity i...