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

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, September 2, 2023

TI-84 Plus CE Python: Drawing Random Paths

TI-84 Plus CE Python: Drawing Random Paths



Introduction



The Python script PATHS draws a path from the left side to one of the other three sides of the screen:  bottom, right, or up.  


The path is laid on a 70 x 70 grid, consisting on 10 x 10 blocks.  Due to the how the screen is set up, the grid does not look like squares.   A random number between 1 and 3 is picked and the path moves in one of three ways:


1:  The path moves up

2:  The path moves right

3:  The path moves down


Three modules are used:


>  random module

>  ti_draw module (exclusive to TI):  drawing commands

>  time module (exclusive to TI):  used for the sleep command



TI-84 Plus CE Python Script:  paths.py


This code should work on the TI-83 Premium CE and the TI-Nspire CX II.  


# draw a random path

# EWS 2023-06-19


from ti_draw import *

from random import *

from time import *



clear()

set_window(0,70,0,70)


# initialization

set_color(0,128,0)

x=0

y=40

# draw using upper left corner

fill_rect(x,y,10,10)


# main loop

while x<60 and y>10 and y<60:

  r=randint(1,3)

  if r==1:

    y+=10

    set_color(0,0,224)

  if r==2:

    x+=10

    set_color(0,128,0)

  if r==3:

    y-=10

    set_color(255,165,0)

  fill_rect(x,y,10,10)

  sleep(.25)


# set end indicator

set_color(0,0,0)

fill_rect(0,40,10,10)

fill_rect(x,y,10,10)


# text - allow for text height

# have to play around

set_color(255,255,255)

draw_text(2,42,"BGN")

draw_text(x+2,y+2,"END")

show_draw()


Notes:


>  The show_draw() command on the end let's us see all the steps.   A sleep command (time module) at the end of the loop to slow down between each execution of the loop.

>  The fill_rect both take coordinates to be the upper-left hand corner of box.  

>  The draw_text works similarly to fill_rect except we have to account for the size of the font.   

>  The set_window command allows us to set the coordinates for the window.  If there is no set_window command, the screen would operate by pixels instead. The TI-84 Plus CE screen is 319 x 209 pixels.  


Three example paths are shown below:  






Eddie 


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


Sunday, June 28, 2020

Numworks Python Scripts: Basic Graphics

Numworks Python Scripts:  Basic Graphics

Script:  atari.py

Draws the basic-eight color palette of the classic 1977 Atari 2600.

Atari Palette Python Script
Atari Palette Python Script


from math import *
from kandinsky import *
# 2020-05-28 atari 2600 colors
# kandinsky module

fill_rect(0,0,320,240,color(245,245,245))

fill_rect(15,15,55,55,color(0,0,0))
fill_rect(85,15,55,55,color(255,0,0))
fill_rect(155,15,55,55,color(255,255,0))
fill_rect(15,85,55,55,color(255,0,255))
fill_rect(155,85,55,55,color(0,255,0))
fill_rect(15,155,55,55,color(0,255,255))
fill_rect(85,155,55,55,color(0,0,255))
fill_rect(155,155,55,55,color(255,255,255))

draw_string("8",107,107)


Script:  firstdigit.py

The tenths digit from n random numbers is extracted and a bar chart is generated based on the results.  I recommend a sample size of at least 20.

firstdigit script example (sample = 100)
A Sample of 100 data points



from math import *
from random import *
from matplotlib.pyplot import *

# set up lists
x=[0,1,2,3,4,5,6,7,8,9]
y=[0,0,0,0,0,0,0,0,0,0]

# user iput
print("EWS 2020-05-29")
print("Bar Chart: First Digit")
print("Recommended at least 20")
n=int(input("n? "))

# generate list
for i in range(n):
  s=int(random()*10)
  y[s]=y[s]+1

# bar plot
h=int(n/2)
d=-int(h/4)
axis([-0.5,9.5,d,h])
bar(x,y)

# turn axis off
axis("off")

# labels at the bottom
# results at top
m=max(y)
for i in range(10):
  text(i-0.25,d+1,str(i))
  text(i-0.25,m+2,str(y[i]))
  
show()

Script:  colorfulrings.py

The script cycles through a set of nine colors, four times.   The Kandinsky module is used to generate the flowery circles as well as cycle through the colors.  This module works with integer pixels.

Color rings script in progress
Color rings script in progress


from math import *
from kandinsky import *
from time import *

# color lists
r=[255,255,255,0,0,0,51,128,255]
g=[0,102,255,128,255,0,102,128,255]
b=[0,0,0,0,0,255,255,128,255]

# angles
a=list(range(128))
for i in range(128):
  a[i]=i/128*2*pi

# draw circles
for k in range(36):
  n=int(fmod(k,9))
  for j in range(50):
    for i in range(128):
      x=int(160+(20+j)*cos(a[i]))
      y=int(120+(20+j)*sin(a[i]))
      set_pixel(x,y,color(r[n],g[n],b[n]))
  sleep(0.1)

Script:  modulusplot.py

Generate a pixel plot of the equation (x^n + y^n) mod m

Modulus Plot example, n = 3.9, m = 15.6
Input Screen  (n = 3.9, m = 15.6)

Modulus Plot result, n = 3.9, m = 15.6
Modulus Plot result, n = 3.9, m = 15.6


from math import *
from kandinsky import *
print("EWS 2020-05-28")
print("x**n + y**n mod m")
n=float(input("power? "))
m=float(input("modulus? "))

for x in range(320):
  for y in range(240):
    t=fmod(pow(x,n)+pow(y,n),m)  
    c=floor(t/m*255)
    set_pixel(x+1,y+1,color(c,c,c))


Eddie

BLOG UPDATE:
The HP Prime:  Conversion to Binary and IEEE-754 Binary blog entry that was posted on June 27, 2020 may contain errors.  In this case, I have taken that entry back to draft status and my intention is to repost the entry as soon as I can.  Apologies for any inconvenience. 

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.

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues The programs are listed for the Swiss Micros DM42 an...