Showing posts with label random integers. Show all posts
Showing posts with label random integers. Show all posts

Saturday, September 19, 2026

Sharp EL-5200/EL-9000 AER II Program Collection – September 2026

Sharp EL-5200/EL-9000 AER II Program Collection – September 2026



For my review on the Sharp EL-5200 (also known as the Sharp EL-9000), check out this blog entry from 2024: https://edspi31415.blogspot.com/2024/04/spotlight-sharp-el-5200.html


The AER programming on Sharp is very unusual.


* Angle mode has to be set manually through the [ 2ndF ] [ FSE ] (DRG) key sequence.

* Similarly, the decimal settings have be set manually through the [ 2ndF ] [ FSE ] key sequence and setting number of decimal places through tab.

* In AER II, there are really only two ways to display messages. First, through the program title when cycling through the programs with the [ PRO ] key. Second by setting a result variable such as area=2×π×r². The display will show “area= (next line) result”.


Graphing Commands


Setting the graphing range:

RANGE xmin, xmax, xstep, ymin, ymax, ystep


Clearing the graph:

G.CL ([2ndF] [ ← ])


Plotting a point:

PLOT x, y

This may include expressions, the point must be in the graph area or an Error 2 occurs.


Drawing a line:

LINE x1, y1, x2, y2

The entire line must fine the graph area or an Error 2 occurs.


Plotting a function:

GRAPH f(X)


Auto Sizing the window (Y-range only):

AUTO

Placed before the DRAW command.


Draw and show the graph screen:

DRAW


Finally, how are loops done?

* Loops are only available on the main portion of the program, and not in the subroutines. I found this out the hard way.

* Use the Y (Yes) and N (No) results to enable the loop.

* There are only four comparisons: =, >, >=, and ≠


A sample syntax:


↳ (loop commands) (comparison) – Y ⇒ [ do if true ↰ ]

↳ (loop commands) (comparison) – N ⇒ [ do if false ↰ ]



All programs today are in AER II mode. Spaces are provided for readability.


Sharp EL-5200: Random Integers


Title: random integers

M: low=? high=? num=?

↳ num>0 -Y→[ INT (RND ×(high-low))+low,

num=num-1 ↰]


Keep on pressing [COMP] until a zero comes up, which signals the end of the list. (This assumes that low and high are both positive numbers.)


Example:


low=15, high=95, num=5

ans=41, 64, 78, 69, 17, 0





Sharp EL-5200: Work Sample Size, 95% Confidence



size = z² ÷ (%acc ÷ 100)² × (1 - #occ ÷ #obs) ÷ #obs × #occ

z: normal distribution value that indicates confidence level; for a 95% interval, z ≈ 1.9599

%acc: desired percent accuracy

#occ: number of occurrences (i.e. idle employees, targets not met, hours missed, etc.) in the preliminary sample

#obs: number of observances in the preliminary sample


Title: work sample size, with 95 percent confidence

M: size = 1.9599² ÷ (occ ÷ 100)² × (1 – occ ÷ obs) ÷ occ × obs ␣

size = INT size + 1,

rat = occ ÷ obs



The size is rounded up to the nearest integer.

rat: ratio of occurrences to observances



Examples:



Example 1:

acc: 10%, occ: 3, obs: 20.

Result: size: 2177, ratio: 0.15



Example 2:

acc: 5%, occ: 2, obs: 108

Result: size: 81,434, ratio: 0.018518519



Source:

Hewlett Packard. “Business Finance and Accounting: Step-by-Step Solutions for Your HP-17B, HP-19B, or HP-27S Calculator”. HP Part No. 00017-90020. Printed in Canada. Edition 3. Corvallis, OR. September 1991. pp. 97-98



Sharp EL-5200: Eigenvalue of a 2 x 2 Matrix



This calculates the eigenvalues of a 2 x 2 matrix, [ [ a, b ] [ c, d ] ] by the following sequence:



m = (a + d) ÷ 2

p = a * d – b * c

Eigenvalues

λ = m ± √(m² – p)



Title: eigen of [[a,b][c,d]] (real)

M: a=? b=? c=? d=?

m=(a+d)÷2 ␣

p=a×d-b×c ␣

m+√(m²-p),

m-√(m²-p)



Example:

[ [10, 8], [3, 6] ]

a=10, b=8, c=3, d=6

Result: 13.29150262, 2.708497378



Source:

3Blue1Brown. “A quick trick for computing eigenvalues | Chapter 15, Essence of linear algebra” YouTube video, posted May 7, 2021. https://www.youtube.com/watch?v=e50Bj7jn9IQ



The next three programs deal with graphics.



Sharp EL-5200: Graphing a Sine Wave



Graph of y = A × sin(B × x + C) given A, B, C. Angles are set to be in radians.



Title: graph A×sin(Bx+C). Set radians mode.

M: A=? B=? C=?

GRAPH A×SIN (B×X+C) AUTO DRAW



Sharp EL-5200: Graphing a Line



Draw a line between (a,b) and (c,d). The program determines the window by determining the maximum and minimum for two numbers:



max(x,y) = (x + y + abs(x – y)) ÷ 2

min(x,y) = (x + y – abs(x – y)) ÷ 2



The distance is displayed.



Title: draw a line between (a,b) and (c,d)

M: a=? b=? c=? d-?

dist=√((c-a)² + (d-b)²),

max=(a+c+ABS(a-c))÷2 ␣

mix=(a+c-ABS(a-c))÷2 ␣

may=(b+d+ABS(b-d))÷2 ␣

miy=(b+d-ABS(b-d))÷2 ␣

RANGE mix-1, max+1, 1, miy-1, may+1, 1 ␣

LINE a, b, c, d DRAW



Sharp EL-5200: Graphing a Rose



This program graphs the rose:

r = cos(5 * Θ) for 0 ≤ Θ ≤ π







Title: graph a rose, set rad mode

M: G.CL ␣ a=0 ␣

RANGE -1.2, 1.2, 0.2, -1.2, 1.2, 0.2 ␣

↳PLOT COS(5×a)×COS a, COS(5×a)×SIN a

DRAW a=a+π÷128 ␣

a>π – N → [ ↰ ]



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, December 6, 2025

Basic vs. Python: Numeric Guessing Games (Featuring Casio fx-702P and fx-CG100)

Basic vs. Python: Numeric Guessing Games


Calculators Used


Basic: Casio fx-702P

Python: Casio fx-CG100


Task


Generate two simple number guessing games.


Guess the Number


This is the classic guess the number. The game generates a number (positive integer) at random in a given range. The player guess the number and if it doesn’t match the target number, the player is told whether the target number is lower or higher. The objective is to find the target number in the lowest number of turns.


The pricing game, The Clock Game, from the legendary game show The Price Is Right uses the Guess the Number game for two prizes. The Clock Game, the contestant needs to get the three-digit price correct for each prize within a total of 30 seconds, with the host (Drew Carey or the late Bob Barker) informing the contest whether the correct price is higher or lower.


The code is for a game where the target integer is between 10 and 99.


BASIC: Casio fx-702P


10 PRT "GUESS THE NUMBER"

20 T=INT (RAN#*90+10)

30 C=0

40 G=0


100 INP "GUESS (10-99)",G

110 IF G<10 THEN 100

115 IF G>99 THEN 100

120 C=C+1

130 IF G<T;PRT "HIGHER"

140 IF G>T;PRT "LOWER"

150 IF G=T THEN 200

160 GOTO 100


200 PRT "CORRECT! THE # IS ";T

210 PRT "# GUESSES: ";C


PYTHON: Casio fx-CG100

Script: numguess.py


from random import *


print("Guess the number ")

t=int(random()*90+10)

c=0

g=0


# != means not

while t!=g:

  g=int(input("Guess (10-99)? "))

  c+=1

  if g<t:

    print("HIGHER")

  if g>t:

    print("LOWER")


# exact guess leaves the loop


print("CORRECT! The # is "+str(t)+".")

print("# of guesses: "+str(c))


The major difference between the two programs is that the Basic version uses If statements and Goto line statements, while Python code uses a while loop.


Find the Coin


This is a guessing game where the player is tasked to find a coin in a 10 by 10 grid. The rows and columns are labeled 0 through 9.





BASIC: Casio fx-702P


10 PRT "FIND THE COIN ($)"

30 A=INT (RAN#*10)

40 B=INT (RAN#*10)

50 C=0

60 PRT "GRID 0-9,0-9"


70 INP "X (0-9)",X

80 INP "Y (0-9)",Y

90 R=ABS (A-X)

100 S=ABS (Y-B)

105 C=C+1

110 IF R=S THEN 200

120 PRT S;" ROW";R;" COL"

150 GOTO 70


200 PRT "YOU FOUND IT!"

210 PRT "SCORE= ";C


PYTHON: Casio fx-CG100

Script: findcoin.py


# find the coin, 10 x 10 grid


from random import *

print("FIND THE COIN")

# random integer from 0 to 9

a=randint(0,9)

b=randint(0,9)

c=0

print("GRID 0-9,0-9")


# set up 

r=-1

s=-2


while r!=s:

  x=int(input("X 0-9: "))

  y=int(input("Y 0-9: "))

  r=abs(a-x)

  s=abs(b-y)

  c+=1

  if r==s:

    break

  print(str(s)+" rows "+str(r)+" col")


print("You found it!")

print("SCORE= ",str(c))


Note: Both numguess.py and findcoin.py use the random module, hence it can be adopted on every calculator with a random module. The HP Prime’s random module is urandom.


Have fun, and modify as you like,


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.


The author does not use AI engines and never will.

Saturday, October 8, 2022

Drawing Random Numbers with Playing Cards 


An Easy Way to Get Random Numbers

Need an easy and fun way to draw random is to use a playing deck of cards.  No electricity or potentially expensive electronic devices are needed, and your shuffling skills will improve dramatically.  





Take a regular deck of cards, and you will only need 40 cards.  Get all the Aces, ones through nines, and one set of face cards.   Assign the following cards as the following values:


Aces:  1

Twos:  2

Threes: 3

Fours: 4

Fives: 5

Sixes: 6

Sevens: 7

Eights: 8

Nines: 9

Any set of face card or tens: 0  (I use Jacks, see the pictures above.  You can use Queens, Kings, or Tens)


To get a random number:

1.  Shuffle the deck

2.  Draw up to four cards 

3.  That card becomes the random number.  Insert a decimal point anywhere if appropriate.  


Why only up to four cards?  There are only four digits available per deck.  We are generating a samples with no replacement.  


Sample four digit numbers:

8-clubs, Ace-hearts, 2-clubs, 5-diamonds:  8125

2-diamonds, Ace-clubs, 3-clubs, 5-spades:  2135

4-hearts, 8-spades, 3-diamonds, 9-spades: 4839

4-hearts, 3-diamonds, 9-slides, 8-clubs: 4398

Jacks-clubs, Ace-clubs, 2-diamonds, 2-spades: 0122


How many possible permutations are possible with this deck?

2 digits:  nPr(40, 2) = 1560

3 digits:  nPr(40, 3) = 59,280

4 digits:  nPr(40, 4) = 2,193,360



Can we create a program to generate random numbers using this method?  

Yes.   I am using the TI-84 Plus CE to for this program.   The TI-84 has a randIntNoRep function, which generates a list of random integers without replacement.   The nice part of the is we are working with base 10, so the use of a remainder function will give the results we want: 0 through 9.  


Syntax:


Random Sample of Integers Without Replacement:

randIntNoRep(low, high, size of the sample)


Remainder function, which can be used as the modulus function.

remainder(x, y):  returns the remainder of x ÷ y


The program RANDDECK will generate a list and draw a histogram.


TI-84 Plus CE Program:  RANDDECK





"2022-08-16 EWS"

ClrHome

Disp "MAKE A 4 DIGIT,","NUMBER FROM A DECK 0-9"

Input "NO OF TRIALS: ", N

For(I,1,N)

randIntNoRep(1,40,4)→L6

remainder(L6,10)→L6

sum(L6*{1000,100,10,1})→N

If I=1

Then

{N}→L5

Else

augment(L5,{N})→L5

End

End

Pause L5

ClrHome

Disp "PRESS [ENTER] TO"

Pause "DISPLAY HISTOGRAM"

Plot1(Histogram,L5,1,BLUE)

PlotsOn 1

ZoomStat


Here is an example.



Until next time,


Eddie


All original content copyright, © 2011-2022.  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, October 30, 2021

Swiss Micros DM41X and HP 41CX: Advanced String Programs

Swiss Micros DM41X and HP 41CX:  Advanced String Programs





Introduction and Generating Random Integers


This blog entry features three programs for the Swiss Micros DM41X and HP 41CX (or a HP 41C with an extended module):


ACODE:  generate a code of random letters


ASAMP:  generate a sample of numbers of digits 0 to 9, where no numbers repeat.  ASAMP accepts up to 9 numbers.   


CRYPT:  allows the user to "add" or "subtract" a code to a word to encrypt it.   CRYPT is a basic form of encryption.   


Both ACODE and ASAMP use a random number generator.  Unfortunately, the HP 41C does not have a random number generator and one must be programmed.  I wanted to avoid reinventing the wheel.   The following formula, obtained from the book An Atlas of Functions (see source), generates random numbers of value 0 ≤ r < 1:


r_n+1 = [ ( (4561 * int(243000 * r_n) + 51349 ) mod 243000 ] ÷ 243000


To convert this into a random integer from a to b: 


randint = int( (b - a + 1) * r) + a


int is the integer function.   The second formula is useful because we don't have to change display modes to execute the calculation.  


Note:


XTOA takes an integer from the stack and appends the associated code to the alpha string.


ATOX takes the left most character, converts it to code, and deposits it on the X stack.  The length of the alpha string is reduced by one.


Codes:

Alphabet:  65 is code for A, 90 is code for Z (all letters inclusive)

Numbers:  48 is code for 0, 57 is code for 57 (all letters inclusive)

# 35

$  36

%  37

&  38

:  58

@  64

[  91

]  93

Σ 126


The system's DATE and TIME are used to generate an initial seed


Swiss Micros DM41X Program:  ACODE


Instructions:

Enter the length, execute ACODE


Example (results will vary):

10 ACODE (may) return CDSJHPNWLD  (10 letters)

11 ACODE -> NVJZJMVVBSV


01 LBL^T ACODE

02 CLA

03 STO 01

04 DATE

05 TIME

06 +

07 2

08 /

09 FRC

10 STO 02

11 LBL 00

12 RCL 02

13 XEQ 01

14 XEQ 02

15 XTOA

16 DSE 01

17 GTO 00

18 AVIEW

19 RTN

20 LBL 01

21 RCL 02

22 243 E3

23 *

24 LASTX

25 X<>Y

26 INT

27 4561

28 *

29 51349

30 +

31 X<>Y

32 MOD

33 LASTX

34 /

35 STO 02

36 RTN

37 LBL 02

38 26

39 *

40 INT

41 65

42 +

43 RTN

44 END


Swiss Micros DM41X Program: ASAMP


Instructions:

Enter the length, execute ASAMP


If the length is greater than 9, an error is generated.  


Example (results will vary):

4 ASAMP can generate results such as 4381, 0361, 4920

7 ASAMP can generate results such as 6732145, 9852067, 1963542

 

Results are returned as an alpha string


01 LBL^T ASAMP

02 CLA

03 STO 01

04 9

05 X<Y?

06 GTO 03

07 DATE

08 TIME

09 *

10 FRC

11 STO 02

12 LBL 00

13 XEQ 01

14 STO 03

15 POSA

16 -1

17 X=Y?

18 GTO 02

19 GTO 00

20 LBL 02

21 RCL 03

22 XTOA 

23 DSE 01

24 GTO 00

25 AVIEW

26 RTN

27 LBL 01

28 RCL 02

29 243 E3

30 *

31 LASTX

32 X<>Y

33 INT

34 4561

35 *

36 51349

37 +

38 X<>Y

39 MOD

40 LASTX

41 / 

42 STO 02

43 10

44 *

45 INT

46 48

47 +

48 RTN

49 LBL 03

50 0

51 1/X

52 RTN

53 END


Swiss Micros DM41X Program:  CRYPT


Syntax:

Store your word in the Alpha register

Give a key (integer), can be positive or negative

XEQ CRYPT


Example:

Starting alpha string:  MATHS

10 CRYPT returns WKDRC

-10 CRYPT returns MATHS (where you started from)

This allows for two people to have short encoded messages and a secret key.


01 LBL^T CRYPT

02 STO 01

03 ALENG

04 STO 02

05 LBL 00

06 ATOX

07 65

08 - 

09 RCL 01

10 +

11 26

12 MOD

13 65

14 + 

15 XTOA

16 DSE 02

17 GTO 00

18 AVIEW

19 RTN 


You can download all three files (in .raw format) here:  

https://drive.google.com/file/d/1UD8CAILnfe4l2ID_UdoHfFGvaYiWni8J/view?usp=sharing


Thanks to Albert Chan (MoHPC) for feedback on the the programs.  



Source for the Random Number Formula:


Keith Oldham, Jan Mayland,  Jerome Spainer  An Atlas of Functions 2nd Edition Springer:  New York, NY.  2009.  ISBN 9780387488066


Eddie


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


Monday, August 9, 2021

Numworks: Finding a Numeric Target Game

Numworks:  Finding a Numeric Target Game


Happy Monday!


Introduction


The Python script target.py is a game where the player is tasked to guess a mystery number.   The game has four levels:


Level 0:  Easy.  Range of 50, number between 100 and 999.

Level 1:  Medium.  Range of 100, number is between 1,000 and 9,999.

Level 2:  Difficult.  Range of 250, number is between 10,000 and 99,999. 

Level 3:  Challenge.  Range of 500, numbers if between 100,000 and 999,999.


After each guess you will be told whether the target is higher or lower than your guess.   At the beginning you are given a range where your target number is.


Trick:  I fit a quartic curve to the points (0,50), (1,100), (2,250), and (3,500) and Numworks came up with y = 50*x^2 + 50.   


The script page on my Numwork's account is here:  https://my.numworks.com/python/ews31415/target


Numworks Python Script: target.py


from math import *

from random import *

# 2021-08-03 EWS

# target finding game


# set up

print("**** TARGET ****")

print(" EWS 2021")

print(" ")

print("SELECT MODE")

print("0. EASY")

print("1. MEDIUM")

print("2. DIFFICULT")

print("3. CHALLENGE")

c=int(input())


# variables

# range

r=50*c**2+50

# lower limit

x=10**(c+2)

# upper limit

y=10**(c+3)-1

# score

s=0

# target, limits

t=randint(x+r,y-r)

lx=t-r

ly=t+r


# the game

g=-1

print("The target is between")

print(str(lx)+" and "+str(ly)+".")


while g!=t:

  s+=1

  g=int(input("Guess "+str(s)+"? "))

  if g>t:

    print("LOWER")

  else:

    print("HIGHER")

  

print(str(g)+" is the target!")

print("Your score is: "+str(s))


Good luck!  


Testing News


According to their website, numworks.com the Numworks calculator is now permitted for SAT, AP, PSAT, and ACT.  Click here for more information:  https://www.numworks.com/calculator/exams/

Note: Some Python Scripts can be Transferred to Different Calculators as is - Check the modules!  

You can download the Python file here.   I was able to transfer the file to a TI-84 Plus CE Python and it runs fine.  Because of the modules used, it should be able to run on any calculator with Python.  Please be aware that the calculator must have the modules installed before attempting to transfer Python scripts between different kinds of calculators.  

Link:  https://drive.google.com/file/d/1g2JjxkeKbk14Pm-qZR_LdW-0B4_BcZik/view?usp=sharing

Eddie


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

7000G Retro Month - June 19 Edition

7000G Retro Month - June 19 Edition





Introduction


Welcome to the 7000G Retro Month, which features programming for the classic Casio calculators from the mid/late 1980s:  primarily fx-7000G and fx-7500G.  Since the programming language stays similar throughout the years, programs can be translated to the fx-6300G and later graphing calculators with little to no adjustments.  Non graphic programs should be ported to the fx-4000P, fx-4500P (A), fx-3650p (II), fx-50F Plus (II), and fx-5800P with little to no adjustments.  


7000G Retro Month takes place every Saturday during June 2021.


To make text easier to type, I can going to use the following text friendly symbols for the following:


->  for →


/I for ⊿


=> for ⇒


What do you think?   Unicode or simple text equivalents?  


- - - - - - -- - -- - -


Today's subject revolves around Probability and Random Numbers.  Enjoy!


- - - -- - - -- - -- -- -


Random Integers: Repeats Allowed 


This program allows the user to generate a number of integers between A and B, repeats are allowed.   Each integer is displayed one at a time.


"A"? -> A

"B"? -> B

"N"? -> N

Lbl 1

Int ((B-A+1) Rnd#) /I

Dsz N

Goto 1


Combinatorics


The program allows the user to choose between three options:


1.  PERM:  permutation:  nPr

2.  COMB:  combination:  nCr

3.  COMB REPLACE:  combination with replacements allowed (n+r-1)Cr


"N"? -> N

"R"? -> R

"1. PERM"

"2. COMB"

"3. COMB REPLACE"

? -> K

K=1 => N!÷(N-R)! -> X

K=2 => N!÷(R!(N-R)!) -> X

K=3 => (N+R-1)!÷(R!(N-1)!) -> X

X


Binomial Distribution


This program calculates the sum of probabilities for a binomial distribution:


total probability = ∑( nCr(N,K) p^K (1-p)^(N-K), K=A to B)


Probability:  0 < p < 1


"A"? -> A : "B"? -> B

"N"? -> N : "P"? -> P

0 -> M : Lbl 1

M+(N!×P^A×(1-P)^(N-A))÷(A!(N-A)!) -> M

A+1 -> A

A>B => Goto 2

Goto 1

Lbl 2

M


Confidence Interval


This program generates a confidence interval using one of four probabilities are assigned to the following variables:


F: 99%  (z* ≈ 2.576)

G: 98% (z* ≈ 2.326)

H: 95% (z* ≈ 1.96)

I: 90% (z* ≈ 1.645)


interval = mean ± z* × variance / √n


"CONF INTERVAL"

2.576 -> F

2.326 -> G

1.96 -> H

1.645 -> I

"MEAN"? -> A

"VAR"? -> B

"N"? -> N

"F=.99, G=.98"

"H=.95, I=.90"

? -> J

A-JB÷√N -> E /I

A+JB÷√N -> F


E:  low, F: high


Eddie


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


Sunday, September 13, 2020

Numworks: Generating a Colorful Matrix

Numworks:  Generating a Colorful Matrix 

(updated 10/18/2021)


Introduction

Today's script generates a 3 x 3 matrix of random integers from 1 - 9 which each of the numbers are assigned a color.  The numbers are displayed in a 3 x 3 grid using the 320 x 240 pixel screen.  

The colors which are assigned to each number are:

1:  Shamrock Green  (0, 158, 96)

2:  Denim Blue (21, 96, 189)

3:  Navy Blue (0, 0, 128)

4:  Indigo (75, 0, 130)

5:  Red (255, 0, 0)

6:  Rose (255, 0, 127)

7:  Brown (101, 67, 33)

8:  Orange (255, 127, 39)

9:  Gold (255, 223, 0)


Numworks Python Script:  colormtx.py


from math import *

from random import *

from kandinsky import *


# 2020-08-25 EWS


# color arrays

# red

mr = [ 0, 21, 0, 75, 255, 255, 101, 255, 255 ]

# green

mg = [ 158, 96, 0, 0, 0, 0, 67, 127, 223 ]

# blue 

mb = [ 96, 189, 128, 130, 0, 127, 33, 39, 0 ]


# set up matrix

mat = [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ]


# generate random numbers, assign colors 

for r in range(3): 

 for c in range(3):

  mat[ r ][ c ] = randint(1,9)

  x = 80 + 80 * c

  y = 60 + 60 * r

  s = mat[ r ][ c ]

  draw_string(str(s), x, y, color(mr[s-1], mg[s-1], mb[s-1]))


Download the code here:  https://my.numworks.com/python/ews31415/colormtx


Gratitude to Brian who let me know about my typo on the last line, it was missing the final right parenthesis.  


Remember that the index of arrays and matrices go from 0 to n-1.  

Examples






Eddie

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


Python: Drawing a Ring of Circles

Python: Drawing a Ring of Circles The goal: Draw a number of circles that revolve around the origin (0,0). The center of each circle i...