Download  Print this page

Advertisement

Program ROLLMANY
Programming
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.
EXPORT ROLLMANY(n,sides)
BEGIN
LOCAL k,roll;
// initialize list of frequencies
MAKELIST(0,X,1,2*sides,1)
FOR k FROM 1 TO n DO
ROLLDIE(sides) + ROLLDIE(sides)
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
L2;
roll;
245

Advertisement

Table of Contents

   Also See for HP 39gII

   Related Manuals for HP 39gII

Comments to this Manuals

Symbols: 0
Latest comments:
  • Peterg Dec 29, 2018 01:11:
    Me interesan los Manuales Hp para usarlo en clases