HP Forums
(12C) Roll Two Dice at Random - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (12C) Roll Two Dice at Random (/thread-10565.html)



(12C) Roll Two Dice at Random - Gamo - 04-24-2018 03:04 AM

Two dice roller program using Random Generator (No Seed Needed) from the previous post.

To roll two dice:
R/S > 0.000000000 // ready to roll...
>1st dice result show briefly
>2nd dice result show briefly
>both dice add up result.

Program:
Code:

0
FIX 9
PSE
FIX 0
RCL 0
355
ENTER
113
÷
+
e^x
FRAC
STO 0
6
x
1
+
INTG
PSE
STO 1
RCL 0
355
ENTER
113
÷
+
e^x
FRAC
STO 0
6
x
1
+
INTG
PSE
RCL 1
+

Gamo


RE: (12C) Roll Two Dice at Random - Dieter - 04-26-2018 07:40 PM

(04-24-2018 03:04 AM)Gamo Wrote:  Two dice roller program using Random Generator (No Seed Needed) from the previous post.
...

Whoa... how long is this? 40 steps or more? And most of the code occurs twice.

OK, the 12C does not features subroutines. So here's another suggestion that uses a "software flag" in R1 to emulate something similar. The method is similar to the f(x) calls in the Newton and Regula Falsi programs. Plus a nifty little trick. ;-)

Code:
01  0
02  GTO 05
03  LSTX
04  1
05  RCL 0
06  9
07  9
08  7
09  x
10  FRAC
11  STO 0
12  6
13  x
14  INTG
15  1
16  +
17  FIX 0
18  PSE
19  x
20  X=0?
21  GTO 03
22  +

The PRNG used here is the classic HP65 generator: r := frac(997 · r). Yes, this requires a seed with 0 < seed < 1 in R0, preferably with many decimals. This method works very well especially for certain seeds, but for rolling two dice I think we can ignore these details. ;-)

If you prefer a different PRNG simply replace it, starting at line 05 – no GTOs have to be adjusted. But be sure that not more than two stack levels are used.

Example:

0,1234567 STO 0

[R/S]  "1"  "1"  2
[R/S]  "5"  "3"  8
[R/S]  "6"  "4"  10
[R/S]  "3"  "6"  9
[R/S]  "2"  "2"  4
...

Edit: here is how it works.

The PRNG starting at line 05 is called twice. First with a zero in x. This way the multiplication in line 19 yields zero, which causes a jump back to step 03. The value of the die still is in LastX, so it can be recalled from there. Then the PRNG is run once again, this time with a one in x. Thus the multiplication in line 19 leaves the value of the second die unchanged. Since this is ≥ 1 the x=0? test is false and the final "+" adds both die rolls together.

Dieter


RE: (12C) Roll Two Dice at Random - Gene - 04-26-2018 07:50 PM

Nevermind. What I posted did not work. It involved STO 1 then STO - 1 to zero the register.


RE: (12C) Roll Two Dice at Random - Dieter - 04-26-2018 08:07 PM

(04-26-2018 07:50 PM)Gene Wrote:  Nevermind. What I posted did not work. It involved STO 1 then STO - 1 to zero the register.

?!? – did I miss something here ?-)

Just edited my version, now it's down to 22 steps.

Dieter


RE: (12C) Roll Two Dice at Random - Gamo - 12-30-2018 10:18 AM

This is the "added on" version from the program on Post #2

This add on included the x.x 00000 xx display

First dice is on the Integer digit
Second dice is on the first decimal digit
Total is on the Exponent

Example:
[R/S] display First Dice Pause then display second Dice and shown
First dice . Second Dice 00000 XX // X is the total of both dice

[R/S] "4" "2" final display [4.2 00000 06]

Program:
Code:

01  0
02  GTO 05
03  LSTX
04  1
05  RCL 0
06  9
07  9
08  7
09  x
10  FRAC
11  STO 0
12  6
13  x
14  INTG
15  1
16  +
17  FIX 0
18  PSE
19  x
20  X=0?
21  GTO 03
22  +
---------------------------
23 X<>Y
24 LSTx
25  1
26  0
27  ÷
28  +
29 X<>Y
30  1
31  0
32 X<>Y
33 Y^X
34  x
35 FIX .   // the dot is the decimal button [.]

Gamo