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.


