Saturday, September 25, 2021

Sharp EL-5500III & PC-1403: Spin a Wheel and Random Samples

 Sharp EL-5500III & PC-1403:  Spin a Wheel and Random Samples


Spin a Wheel

With the use of the WAIT command, a FOR loop, and an array of strings, we can simulate the spin of a wheel of a random number of steps.  

Note, any variable can be designated a string by adding a dollar sign to the letter, for example A stores a real number, while A$ is a string. 

In an array, A(n-1) has n elements, designated A(0) to A(n-1).  Example:  A(3) has three elements, A(0), A(1), and A(2).

The program presented has a 12 space wheel.  

Sharp EL-5500III/PC-1403 Program:  Spin a Wheel
RUN 900 (or whatever line you designate)

900 DIM A$(11): REM SPIN THE WHEEL
903 N=15 + RND(30)
906 I=RND(12)-1: REM POINTER
909 REM RND(N) GETS 1 TO N
910 A$(0)="$  500"
911 A$(1)="$  650"
912 A$(2)="$  600"
913 A$(3)="$  800"
914 A$(4)="$  750"
915 A$(5)="LOST TURN"
916 A$(6)="$  900"
917 A$(7)="BONUS PRIZE"
918 A$(8)="$  550"
919 A$(9)="$  700"
920 A$(10)="$1,000"
921 A$(11)="  BROKE"
930 WAIT 30: PRINT "SPIN!"
933 FOR J=1 TO N
936 WAIT 20: PRINT A$(I)
939 I=I+1
942 IF I>11 THEN LET I=0
945 NEXT J
946 REM WAIT W/O TIME RESETS PRINT
948 WAIT: PRINT "YOUR SPIN: ";A$(I)
951 END

RUN 900 in RUN mode will show you a wheel spinning.  One of the best results:  "YOUR SPIN: $1,000".   Good luck and have fun!

Random Sample

This program generates an array from a random sample of integers from 1 to N without replacement.  

To save room, a single array is created and an iterative search is made to check to leave out duplicates.   This will increase the time needed to generate longer lists.

Sharp EL-5500III/PC-1403 Program: Random Sample
RUN 1000 (or whatever line you designate)

1000 PRINT "RANDOM SAMPLE"
1003 INPUT "1 TO N. N? "; N 
1006 INPUT "SIZE? "; S
1009 DIM L(S-1)
1012 FOR I=0 TO S-1
1015 R=RND(N)
1018 REM CHECK
1021 FOR J=0 TO I
1024 IF L(J)=R THEN 1015
1027 NEXT J
1030 L(I)=R
1033 PRINT "L("; I; "): "; R
1036 NEXT I
1039 PRINT "RESULT IN ARRAY L"
1042 END

Example:
Choose 10 numbers from 1 to 45.  

1 TO N.  N?:  45
SIZE? 10

A result (your results will vary):
L(0): 9
L(1): 5
L(2): 8
L(3): 4
L(4): 41
L(5): 26
L(6): 42
L(7): 11
L(8): 10
L(9): 28
RESULTS IN ARRAY L

Swiss Micros DM41X Month:  October 2021

Every Saturday in October 2021 will feature the wonderful Swiss Micros DM41X (which programs will apply to the DM41X, DM41L, and the classic HP 41C family).   See you on the next series! 


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. 

No comments:

Post a Comment

Σ(1 / (a^n)) from n=1 to m

 Σ(1 / (a^n)) from n=1 to m This blog entry covers the sum of the series: Σ[1 / (a^n), n=1 to m] with n and m positive integers Specific Cas...