Sunday, September 13, 2026

HP 71B Basic and Casio fx-CG 100: Weighted Random Sample

HP 71B Basic and Casio fx-CG 100: Weighted Random Sample




Introduction



In calculators, it is fairly easy to generate a random sample when every number or object has an equal chance to be picked. But what happens when this is not the case?


Let’s compare two bags:


Bag A has one red marble, one blue marble, one green marble, and one gold marble.


Bag B has one red marble, three blue marbles, two green marbles, and one gold marble.


Bag A has an equal amount of colored marbles, while Bag B doesn’t.


We could easily use a list to represent Bag B as such:

[red, blue, blue, blue, green, green, gold]


That is well and good when the population is small. Let’s consider a larger population:


Box C has 100 red marbles, 75 blue marbles, 100 green marbles, and 125 gold marbles. If we took the approach like we did with Bag B, we would have a list of 400 elements. Some calculators don’t even allow a list with 400 elements! And if they do, that list might take a lot of memory. This calls for a different approach.



An Approach to Consider



Consider creating a table of cumulative probabilities. Then we can use a standard random number (psuedo)generator function, which generates random number between 0 (inclusive) and 1 (not inclusive).


We will take Box C as an example. Recall that Box C has 100 red marbles, 75 blue marbles, 100 green marbles, and 125 gold marbles.


Calculate the probability of picking only one marble out of the bag.


Color

# of Marbles

Probability

Red

100

0.25

Blue

75

0.1875

Green

100

0.25

Gold

125

0.3125

Total

400



Note that sorting is not required. The next step is to calculate the cumulative probability.


Color

# of Marbles

Probability

Cumulative Probability

Red

100

0.25

0.25

Blue

75

0.1875

0.4375

Green

100

0.25

0.6875

Gold

125

0.3125

1

Total

400




The probability intervals can be set up as:


Red: 0 ≤ x < 0.25

Blue: 0.25 ≤ x < 0.4375

Green: 0.4375 ≤ x < 0.6875

Gold: 0.6875 ≤ x < 1


General a random number, for example: 0.344. Since 0.344 lies in between 0.25 and 0.4375, this corresponds to a blue marble.


Let’s say the random number is 0.678. Since 0.678 lies in between 0.4375 and 0.6875, this corresponds to a green marble.



The HP 71B Basic Problem WSAMPLE


The program is uses a container of four colored marbles: red, blue, green, and gold. Provide the number of marbles for each color and the sample size. Each pick is shown to be separately noted on paper or computer.


Note that this a sample with replacement (whatever is picked is returned to the population, which could allow repeats).


Code (365 bytes):


100 DESTROY ALL

105 OPTION BASE 1

110 DIM S$(4)[5]

115 DIM W(4)

120 S$(1)="RED"

125 S$(2)="BLUE"

130 S$(3)="GREEN"

135 S$(4)="GOLD"

140 DISP "# OF MARBLES?" @ WAIT .5

145 T=0

150 FOR I=1 TO 4

155 DISP S$(I) @ WAIT .5

160 INPUT W(I)

165 T=T+W(I)

170 NEXT I


200 U=0

205 DISP P(4),C(4)

210 FOR I=1 TO 4

215 P(I)=W(I)/T

220 U=U+P(I)

225 C(I)=U

230 NEXT I


300 INPUT "SIZE? ";N

305 FOR I=1 TO N

310 R=RND

315 J=1

320 X=C(J)

325 IF R>=X THEN 400

330 DISP STR$(I)&": "&S(J) @ PAUSE

335 NEXT I

340 END


400 J=J+1

405 GOTO 320



Examples (results will vary):


Example 1: (n = 5)

1: GREEN

2: RED

3: GOLD

4: RED

5: GREEN


Example 2: (n = 5)

1: BLUE

2: GREEN

3: GREEN

4: RED

5: GREEN


Example 3: (n = 10)

1: RED

2: GOLD

3: BLUE

4: BLUE

5: GREEN

6: GREEN

7: GOLD

8: RED

9: RED

10: GREEN



Casio fx-CG 100 Python: Weighted Random Sample



This program script, wsample.py, asks for a list of labels and the population for each label. This allows flexibility for additional applications.


Note that this a sample with replacement (whatever is picked is returned to the population, which could allow repeats).


Code:



# EWS 2026-09-05

from math import *

from random import *


print("Weighted Random Sample")

s=eval(input("Label List? "))

w=eval(input("Weights List? "))

n=int(input("Sample Size? "))


# determine probabilities

p=[i/sum(w) for i in w]


# cumulative sums

c=[sum(p[:i+1]) for i in range(len(p))]


# build sample

x=[]

for i in range(n):

  r=random()

  i=0

  while r>=c[i]:

    i+=1

    # end while

  x.append(s[i])

  # end for  


print(x)



Example (results will vary):


Example 1:

labels: [“red”, “blue”, “green”, “yellow”]

weights list: [ 5, 15, 10, 5 ]

sample size: 10

result: [“yellow”, “green”, “green”, “blue”, “yellow”, “green”, “yellow”, “green”, “red”, “green”]


Example 2:

labels: [“purple”, “orange”, “teal”, “gray”, “mint”]

weights list: [10,20,10,10,20]

sample size: 10

result: [“mint”, “gray”, “teal”, “purple”, “orange”, “purple”, “gray”, “mint”, “orange”, “orange”]



Hopefully this approach is useful.


Eddie


All original content copyright, © 2011-2026. 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, September 12, 2026

Trigonometry Reduction Formula and Solving Simple Arcsine and Arccosine Equations

Trigonometry Reduction Formula and Solving Simple Arcsine and Arccosine Equations




Some Background and Periodic Reduction Formulas


This blog will focus on angle measurement in degrees. For radians and grads, please use the appropriate measurement.


90° = Ï€/2 rad = 100 grad

180° = Ï€ rad = 200 grad


sin 90° = 1, sin 180° = 0, sin 360° = 0, sin(-x) = -sin(x)

cos 90° = 0, cos 180° = -1, cos 360° = 1, cos(-x) = cos(x)


General Sums


sin(α + ß) = sin α * cos ß + sin ß * cos α


sin(α + 90°) = sin α * cos 90° + sin 90° * cos α = cos α

sin(α + 180°) = sin α * cos 180° + sin 180° * cos α = -sin α

sin(α + 360°) = sin α * cos 360° + sin 360° * cos α = sin α

sin(90° - α) = sin 90° * cos(-α) + cos 90° * sin(-α) = cos(-α) = cos α

sin(180° - α) = sin 180° * cos(-α) + cos 180° * sin(-α) = -1 * -sin α = sin α


cos(α + ß) = cos α * cos ß – sin α * sin ß


cos(α + 90°) = cos α * cos 90° – sin α * sin 90° = -sin α

cos(α + 180°) = cos α * cos 180° – sin α * sin 180° = -cos α

cos(α + 360°) = cos α * cos 360° – sin α * sin 360° = cos α

cos(90° - α) = cos 90° * cos(-α) – sin 90° * sin(-α) = -sin(-α) = sin α

cos(180° - α) = cos 180° * cos(-α) – sin 180° * sin(-α) = -1 * cos(-α) = -cos α



The Trigonometric Reduction Formula


a * sin(x) + b * cos(x) = √(a² + b²) * sin(x + Θ)


Let x = 90°:

a * sin(90°) + b * cos(90°) = √(a² + b²) * sin(90° + Θ)

a * 1 + b * 0 = √(a² + b²) * cos(Θ)

⇒ cos(Θ) = a ÷ √(a² + b²)


Let x = 180°:

a * sin(180°) + b * cos(180°) = √(a² + b²) * sin(180° + Θ)

a * 0 + b * -1 = √(a² + b²) * -sin(Θ)

-b = -sin(Θ) * √(a² + b²)

⇒ sin(Θ) = b ÷ √(a² + b²)


Then:

sin(Θ) = b ÷ √(a² + b²)

cos(Θ) = a ÷ √(a² + b²)

[sin(Θ) ÷ cos(Θ)] = [ b ÷ √(a² + b²) ] ÷ [ a ÷ √(a² + b²) ]

tan(Θ) = b ÷ a

Θ = arctan(b ÷ a)





Wikibooks uses the Algebraic Argument (see source):



a * sin(x) + b * cos(x)

= [ √(a² + b²) ÷ √(a² + b²) ] * [ a * sin(x) + b * cos(x) ]

= √(a² + b²) * ( a ÷ √(a² + b²) * sin(x) + b ÷ √(a² + b²) * cos(x) )

= √(a² + b²) * ( cos(Θ) * sin(x) + sin(Θ) * cos(x) )

= √(a² + b²) * sin(x + Θ)



Solving Simple Arcsine Equations


The calculator arcsine function gives: Domain: -1 ≤ x ≤ 1, Range: -90° ≤ Θ ≤ 90°


Note that for any angle x: sin(180° - x) = sin(x), sin(x) = sin(x ± 360°*z) (z is an integer)


Given n, solve for Θ:

n = sin(Θ) = sin(180° - Θ)


Base Solution 1:

n = sin(Θ)

⇒ Θ = arcsin(n)


Base Solution 2:

n = sin(180° - Θ)

arcsin(n) = 180° - Θ

⇒ Θ = 180° - arcsin(n)


Example:

0.67 = sin(Θ)

Base Solution 1: Θ = arcsin(0.67) ≈ 42.0670648025°

Base Solution 2: Θ = 180° - arcsin(0.67) ≈ 137.932935198°


Given n and α, solve for Θ:

n = sin(α + Θ)


Base Solution 1:

n = sin(α + Θ)

arcsin(n) = α + Θ

⇒ Θ = arcsin(n) – α


Base Solution 2:

n = sin(180° - (α + Θ))

n = sin(180° - α – Θ)

arcsin(n) = 180° - α – Θ

⇒ Θ = 180° - α – arcsin(n)


Example:

0.7757 = sin(Θ + 76°)

Base Solution 1: Θ = arcsin(0.7757) – 76° ≈ -25.1314549842°

Base Solution 2: Θ = 180° - 76° - arcsin(0.7757) = 104° - arcsin(0.7757) ≈ 53.131459842°


To get all the possible angles, add and subtract multiples of 360°.


Solving Simple Arccosine Equations


The calculator arccosine function gives: Domain: -1 ≤ x ≤ 1, Range: -90° ≤ Θ ≤ 90°


Note that for any angle x: cos(180° - x) = -cos(x), cos(x) = cos(x ± 360°*z) (z is an integer)


Given n, solve for Θ:

n = cos(Θ), n = cos(-Θ)


Base Solution 1:

n = cos(Θ)

⇒ Θ = arccos(n)


Base Solution 2:

n = cos(-Θ)

⇒ Θ = -arccos(n)


Example:

0.58 = cos(Θ)

Base Solution 1: Θ = arccos(0.58) ≈ 54.54945736°

Base Solution 2: Θ = -arccos(0.58) ≈ -54.54945736°


Given n and α, solve for Θ:

n = cos(α + Θ)


Base Solution 1:

n = cos(α + Θ)

arccos(n) = α + Θ

⇒ Θ = arccos(n) – α


Base Solution 2:

n = cos(-(α + Θ))

n = cos(-α – Θ)

arccos(n) = -α – Θ

-arccos(n) = α + Θ

⇒ Θ = -arccos(n) – α


Example:

0.6 = cos(35° + Θ)

Base Solution 1: Θ = arccos(0.6) – 35° ≈ 18.13012035°

Base Solution 2: Θ = -arccos(0.6) – 35° ≈ -88.13010235°



To get all the possible angles, add and subtract multiples of 360°.



Sources

Sterling, Mary Jane. Trigonometry for Dummies. 2nd Edition. John Wiley & Sons, Inc. Hoboken, NJ. 2014. pp. 351-352. ISBN 978-1-118-82741-3


“Trigonometry/Simplifying a sin(x) + b cos(x)”. Wikibooks. January 2, 2024. Retrieved January 4, 2026. https://en.wikibooks.org/wiki/Trigonometry/Simplifying_a_sin(x)_%2B_b_cos(x)


Eddie


All original content copyright, © 2011-2026. 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, September 5, 2026

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 in ft/sec^2 given:



1. The observer’s latitude on Earth (North or South; you don’t have to enter negative numbers for South)

2. The elevation where the observer is in feet.



The Python script is made with a TI-84 Evo, but it should work with all platforms with Python.



Equation Used: With Everything Converted to U.S. Units:



Calculating g_0 at sea level with latitude φ:

g_0 = 32.17192 – 0.08530 * cos(2 * φ°) = 32.17192 – 0.08530 * cos(2 * φ * Ï€ ÷ 180)



Calculating g_h with height h:

g_h = g_0 * (erad ÷ (erad + h ÷ 5280))^2



Where (Earth’s radius at latitude φ):

erad = √((a^4 + b^4 * tan(φ)^2) ÷ (a^2+ b^2 * tan(φ)^2)) 



a = 3963.1905919

b = 3949.90276423189



Code: gravus2.py


'''

Earth's Gravity in US Units

EWS 05/26/2026

'''


from math import *


# title screen

print("Earth's Gravity")

print("Given Latitude and Elevation")

print("U.S./Imperial Units")

print("-----------------")


# angles must be converted to radians


print("Latitude in DMS:")

l1=eval(input("Whole Degrees? "))

l2=eval(input("Whole Minutes? "))

l3=eval(input("Seconds? "))

langle=l1+l2/60+l3/3600

# convert to radians

langle*=pi/180

# elevation

elev=eval(input("Elevation in feet? "))

# calculate radius 

a=3963.1905919

b=3949.90276423189

erad=(a**4+b**4*(tan(langle))**2)

erad/=(a**2+b**2*(tan(langle))**2)

erad=sqrt(erad)

# calculation

g=32.17192-0.08530*cos(2*langle)

g*=(erad/(erad+elev/5280))**2

print("Earth's radius: {0:.5f} mi".format(erad))

print("Estimated Gravity: {0:.5f} ft/s**2".format(g))



Examples (Results are rounded to five decimal places)



Latitude (φ)

Height (h ft)

Earth’s Radius (mi)

g_h (ft/s^2)

34°03’00”

500

3959.04875

32.13857

64°11’27”

1650

3952.43868

32.21979

10°57’00”

395

3962.71501

32.09156





Source



Grainger Engineering Office of Marketing and Communications. (answer written by Rebecca H.) (2016, November 21). “How gravitational force varies at different locations on Earth.” Illinois. https://van.physics.illinois.edu/ask/listing/64061. Retrieved March 10, 2026.

Planetcalc. “Earth Radius by Latitude (WGS 84)” Timur. 2021. https://planetcalc.com/7721/ Retrieved March 22, 2026.



Eddie


All original content copyright, © 2011-2026. 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, August 29, 2026

The MU Key on a Four Function Calculator and Programs for the DM42/HP 42S

 The MU Key on a Four Function Calculator and Programs for the DM42/HP 42S


Not too long ago, I purchased this very colorful, four function calculator from the University I work at, the Dexin BST DX-818.





The DX-818 is powered by a solar panel with an option to be run by an AAA battery when using the calculator in less than bright light conditions. The AAA battery is optional.



A Standard Calculator



This calculator is the standard four function key calculator, complete with the square root function, percent function, the standard three memory keys (M+, M-, and MRC), and a grand total (GT) key. A grand total is started when calculations are completed by pressing the equals key [ = ]. The grand total is both recalled and cleared when the [ GT ] is pressed.



The Markup Equation



Before we head to the calculators, the mark up is a calculation of between selling price (new), cost (old), and mark up. The markup is ratio between the selling price and cost over cost and as expressed as a percentage (%):



MU = (selling price – cost) ÷ cost = (selling price ÷ cost - 1)



Solving for the other variables yield:

selling price = cost * (1 + MU)

cost = selling price ÷ (1 + MU)

In these equations, MU is expressed as a decimal.



If let the selling price be the new value and cost be the old value, then we have the equation for percent change (Δ%):

Δ% = (new – old) ÷ old = (new ÷ old - 1)

Similarly:

new = old * (1 + Δ%)

old = new ÷ (1 + Δ%)

In these equations, Δ% is expressed as a decimal.



The Markup Key (MU)



On this particular calculator, there is an [ MU ] key. This is the markup key. Apparently, there are two types of [ MU ] key, one set of syntax for Casio calculators with the [ MU ] key. An example of a Casio calculator with an [ MU ] key is the MJ-120D plus. They way Casio calculators use the [ MU ] key is a calculation:



cost [ MU ] markup percentage [ % ] returns selling price.

Immediately pressing [ = ] returns selling price – cost.



However, some calculators, like the colorful calculator featured (I really wish I knew the model number) with the [ MU ] have another syntax, and that is what I will address from here on out.





On this particular calculator, the syntax, using one of the four arithmetic functions is:


y < +, -, ×, or ÷ > x [ MU ]


No use of the equals key is needed, unless you want to add the result to the grand total.



The four calculations with the [ MU ] key, on this particular calculator (and possibly the NewYes calculators from China) are:



Key Sequence

Formula Used

Notes

Y [ + ] X [ MU ]

100 * (Y ÷ X + 1)


Y [ - ] X [ MU ]

100 * (Y ÷ X - 1)

Calculates markup or percent change (Δ%).

Y = selling price, new value

X = cost, old value

Y [ × ] X [ MU ]

Y * (1 + X ÷ 100)

Adds X% to Y. Calculates the new value (selling price) given old value (cost) (Y) and markup (X).

Y [ ÷ ] X [ MU ]

Y ÷ (1 – X ÷ 100)

Finds the original retail price given selling price (Y) and discount rate (X). This can also be used for gross-up calculations.



Sample Calculations



Problem

Keystrokes

Result

Determine 100 * (Y ÷ X + 1) with Y = 420, X = 25

420 [ + ] 25 [ MU ]

1780

An item that cost the company $14.75 sells for $19.99. Find the markup.

19.99 [ - ] 14.75 [ MU ]

35.5254237288

(≈35.53%)

A company manufactures a product with a unit cost of $32.84 and wants a 20% markup. What is the selling price?

32.84 [ × ] 20 [ MU ]

39.408

(about $39.41)

A product is currently on sale for $39.99. If the discount is 35%, what is the original retail price.

39.99 [ ÷ ] 35 [ MU ]

61.523076923

(about $61.52)




RPN Programming with the Swiss Micros DM42



Here are four RPN programs, which are were programmed using a Swiss Micros DM42, which can be used on the HP 42S (and probably the HP 41C).



Program MU+: Y [ + ] X [ MU ]

00 {17-Byte Prgm}

01 LBL “MU+”
02 ÷

03 1

04 +

05 100

06 ×

07 RTN

08 END



Program MU-: Y [ - ] X [ MU ]

00 {17-Byte Prgm}

01 LBL “MU-”
02 ÷

03 1

04 -

05 100

06 ×

07 RTN

08 END



Program MUMLT: Y [ × ] X [ MU ]

00 {12-Byte Prgm}

01 LBL “MUMLT”
02 %

03 +

04 RTN

05 END



Program MUDIV: Y [ ÷ ] X [ MU ]

00 {21-Byte Prgm}

01 LBL “MUDIV”
02 ENTER

03 100

04 ÷

05 1

06 X<>Y

07 -

08 ÷

09 RTN

10 END



Sources


 Casio. “MJ-120D Plus” https://www.casio.com/intl/basic-calculators/product.MJ-120DPLUS/ Accessed May 23, 2026.


Eaton, Glenn. “The Calculator MU Key” Eaton Family Website. Posted on June 30, 2017. https://eatonfamily.au/calculator-mu-key/ Accessed May 23, 2026.


“How to use the MU button on a calculator? #1” YouTube Short: @newyes1162. Posted December 5, 2022. https://www.youtube.com/shorts/OiS8yFKKJSU Accessed May 23, 2026


Eddie


All original content copyright, © 2011-2026. 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.


HP 71B Basic and Casio fx-CG 100: Weighted Random Sample

HP 71B Basic and Casio fx-CG 100: Weighted Random Sample Introduction In calculators, it is fairly easy to generate a rand...