Programming
245
Any statements between the end of the
RETURN
statement
and
END
are ignored.
On the Home screen (or in fact, anywhere in the
calculator where a number can be used), you can enter
ROLLDIE(6)
and a random integer between 1 and 6,
inclusive will be returned.
Another program could use the
ROLLDIE
function, and
generate
n
rolls of a die with any number of sides. In the
following program, the
ROLLDIE
function is used to
generate
n
rolls of 2 dice, each with the number of sides
given by the local variable sides. The results are stored
into the list L2, so that L2(1) shows the number of times the
dies came up with a 1, L2(2) shows the frequency of 2's,
etc. L2(1) should be 0 as a result.
Program ROLLMANY
EXPORT ROLLMANY(n,sides)
BEGIN
LOCAL k,roll;
// initialize list of frequencies
MAKELIST(0,X,1,2*sides,1)
L2;
FOR k FROM 1 TO n DO
ROLLDIE(sides) + ROLLDIE(sides)
roll;
L2(roll)+1
L2(roll);
END;
END;
This program uses a
FOR
loop, explained in the section
on loops.
A function's visibility can be restricted to within the
program where it is defined by omitting the
EXPORT
command when the function is declared. For example,
you could define the
ROLLDIE
function inside the
ROLLMANY
program like this:
EXPORT ROLLMANY(n,sides)
BEGIN
LOCAL k,roll;
// initialize list of frequencies
Summary of Contents for 39gII
Page 1: ...HP 39gII graphing calculator user s guide Edition1 Part Number NW249 90001 ...
Page 2: ...Printing History Edition 1 November 201 1 ...
Page 32: ...22 Getting started ...
Page 58: ...48 Apps and their views ...
Page 70: ...60 Function app ...
Page 80: ...70 Solve app ...
Page 136: ...126 Polar app ...
Page 140: ...130 Sequence app ...
Page 148: ...138 Finance app ...
Page 152: ...142 Linear Solver app ...
Page 218: ...208 Matrices ...
Page 226: ...216 Notes and Info ...
Page 238: ...228 Variables and memory management ...
Page 318: ...308 Programming ...
Page 340: ...330 Reference information ...
Page 346: ...vi Product Regulatory Information ...