Post Reply 
Sum of roll of N dice
05-11-2018, 09:52 AM
Post: #41
RE: Sum of roll of N dice
You guys can have your probability-based solutions. I do have a perfectly working version of the program from the DUP 6. SWAP post that I've extended for n-sided dice. Dies. Dice... eh, whatever:

Code:

« \-> Dcount Dsize
  «  Dcount DUP Dsize SWAP ^ RAND * FLOOR
      DO Dsize IDIV2 ROT + SWAP DUP NOT
      UNTIL
    END DROP
  »
»

Now I'm trying to get the later version (OVER 1 PICK3) working in the same way, but I seem to be doing something wrong. I'll get back to this thread once I've worked out where in the dingoes I'm going wrong with my program. And yes, I'm aware of the limitations of RAND and Y^X expressed earlier in the thread.

On a related note, I'm now trying to figure out how to jack up the speed of the kermit interface up to 38400. All I seem to get currently is 9600 with no way to change it. Using "set speed 38400" merely results in "?SET SPEED fails, speed is 9600". I haven't seen any obvious way to get the calculator to change this either.

(Post 217)

Regards, BrickViking
HP-50g |Casio fx-9750G+ |Casio fx-9750GII (SH4a)
Visit this user's website Find all posts by this user
Quote this message in a reply
05-11-2018, 12:19 PM
Post: #42
RE: Sum of roll of N dice
(05-11-2018 09:52 AM)brickviking Wrote:  You guys can have your probability-based solutions. I do have a perfectly working version of the program from the DUP 6. SWAP post that I've extended for n-sided dice. Dies. Dice... eh, whatever:

Your generalized solution (and the base-6 idea from Jim Horn)
(05-02-2018 07:01 AM)Jim Horn Wrote:  If you roll N six sided dice and put them next to each other and write down the values they show *minus 1* each, you'll get an N digit integer in base 6. All such 6^N integers are equally likely for fair dice. So, just find a random number from 0 to (6^N)-1, convert to base 6, add the sum of its digits plus N (to correct the "subtract 1 from each digit") and there you go: the sum of the six rolled dice with only one random number generation.

are beautifully elegant- maybe the best there is! In my own problem solving I do struggle with knowing when is "good enough" and when to keep looking for a more elegant algorithm. Sometimes I learn new and equally cool things when I continue a little further down a path, even if I have a working solution in hand.

We are all explorers, and serendipity is waiting just around the corner. Smile

17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b

Find all posts by this user
Quote this message in a reply
06-23-2018, 09:36 PM
Post: #43
RE: Sum of roll of N dice
(05-02-2018 07:01 AM)Jim Horn Wrote:  So, just find a random number from 0 to (6^N)-1, convert to base 6, add the sum of its digits plus N (to correct the "subtract 1 from each digit") and there you go: the sum of the six rolled dice with only one random number generation.

We don't really need to convert to base 6 if we only want the sum of the digits.
Here's a small program for the HP-42S:
Code:
6
RCL ST Y
Y↑X
RAN
×
IP
+
0
LASTX
LBL 00
X=0?
GTO 01
6
÷
IP
+
LASTX
GTO 00
LBL 01
R↓
5
×
Just enter N and it calculates the sum.

The program works similar to the Base Conversion but instead of multiplying the digits by powers of 10 they are just added.

Let's assume we want to calculate the digits of the number 13710 = 3456.
So we divide 137 consecutively by 6 and get 22 and then 3.
This allows to represent the digits as:
Code:
5 = 137 - 6 · 22
4 = 22 - 6 · 3
3 = 3
The sum of the digits is then:
Code:
5 + 4 + 3 = 137 - 5 · 22 - 5 · 3 = 137 - 5(22 + 3) = 12
Thus we don't have to calculate the remainder after the division by 6.
Find all posts by this user
Quote this message in a reply
06-23-2018, 10:33 PM
Post: #44
RE: Sum of roll of N dice
Thomas! Welcome back to MoHPC!

It has been a (very!) long time since you've been around; I wanted to say you have been missed. I'm really glad to see you're back.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
06-24-2018, 01:08 AM
Post: #45
RE: Sum of roll of N dice
I have used a fairly fast method for rolling 5 or 6 dice. For 5 dices, I build a random number generator with modulus M=6 ^5*2^24. I used a Lehmer type. So after finding a good A (I don't have a numerical A here; the idea is that A/M as continued fraction should have small partial quotients), I just compute X=X*A+1 Mod M. Then dividing X by 2^24, I get a number uniformly distributed between 0 and 7775; convert to base 6 (or work the base 6 stuff into the computation). One gets a uniform distribution over five 6s. That's the roll.

This works if one has a good big-integer package.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)