Page 1 of 1

Dice Roller Example

PostPosted: Tue May 01, 2018 7:30 pm
by ElliotM
I made some dice which you can roll to illustrate how you can use Rand(0,X) to simulate dice rolling. I store the result in an integer property on the dice which you can examine after you have rolled once. If you look in the roll dice task you will see I make use of %number1% and %number2% to capture the number of dice and how many sides they should have. Restrictions enforce realistic dice of 4, 6, 8, 12, 20, and 100 sides.

Dice Roller.taf
You have some dice, what will you roll?
(15.37 KiB) Downloaded 55 times


adrift Code: Select all
Game Parlor
You can roll dice here using the syntax XDY, where X is the number of dice and Y is how many sides it has. I have restricted it to realistic dice of 4, 6, 8, 12, 20, and 100 sides.
 
For example, 'roll 3D6' would roll three six-sided dice, giving you a sum total between 3 and 18.    
 
> roll 3d6
You rolled 3 6-sided dice and got 6.
 
> roll 3d6
You rolled 3 6-sided dice and got 10.
 
> roll 3d6
You rolled 3 6-sided dice and got 13.
 
> x dice
An assortment of dice with different numbers of sides used for gaming.
 
To use them try 'roll xDy' where x is the number of dice you want to roll and y is the number of sides each dice should have.
 
Your last roll result was 13.
 
> roll 2d100
You rolled 2 100-sided dice and got 67.
 
> roll 5d20
You rolled 5 20-sided dice and got 98.
 
> roll 3d9
You only have 4, 6, 8, 12, 20, or 100 sided dice to roll with.

Re: Dice Roller Example

PostPosted: Wed May 02, 2018 1:50 pm
by Campbell
Just as an aside, I'd keep clear of using %number%, %number1%, %number2% etc unless you specifically mean to, as those are the referenced number functions, and reusing them as variables is very likely to cause conflict.

Re: Dice Roller Example

PostPosted: Wed May 02, 2018 3:50 pm
by ElliotM
I didn't think it was possible to reuse them as variables but even if that were possible I would agree that is a horrible idea because they get wiped anytime a task command syntax using them gets processed.

Here is how I used them in the Task Action:

adrift Code: Select all
Set property 'RollResult' of object 'the dice' to 'Rand(0, %number1% * %number2%) + 1'


and here is how I used them in the task completion output:

adrift Code: Select all
You rolled %number1% %number2%-sided dice and got Dice.Rollresult.


Both instances are in the same task, so there shouldn't be a chance that they get altered before I need them.

Re: Dice Roller Example

PostPosted: Wed May 02, 2018 9:37 pm
by ralphmerridew
That's not what "#d#" means. Elliot's method will produce numbers evenly distributed over the range, but if there's more than one die thrown, the correct interpretation should be biased towards the center.

Re: Dice Roller Example

PostPosted: Wed May 02, 2018 11:24 pm
by ElliotM
I'll probably have to tweak the math now that I look at it. The more 'physically' accurate formula should probably be this (at least when it comes to modeling the minimum possible roll):

adrift Code: Select all
Rand(%number1%, (%number1% * %number2%) + 1)

Re: Dice Roller Example

PostPosted: Thu May 03, 2018 12:01 am
by Denk
As Ralphmerridew pointed out, ElliotM's taf-file produce numbers evenly distributed. I tried to modify the file so that the distribution becomes correct, by using loops. The modified file is inserted below.

It would be nice if it was possible to avoid loops, which makes the taf-file more complicated, but I don't think it is possible to get the correct distribution without loops.

Re: Dice Roller Example

PostPosted: Thu May 03, 2018 12:37 am
by ElliotM
ralphmerridew wrote:That's not what "#d#" means. Elliot's method will produce numbers evenly distributed over the range, but if there's more than one die thrown, the correct interpretation should be biased towards the center.


I see what you're referring to now. Does Denk's modification have this center bias?

Re: Dice Roller Example

PostPosted: Fri May 04, 2018 10:38 pm
by Denk
ElliotM wrote:Does Denk's modification have this center bias?
Yes, it has center bias when rolling two or more dice.

As an example, lets take 2D4.
In my file, the result is simply found as:

2D4 result = rand(1,4)+rand(1,4)

Each rand(1,4) represents the roll of one die.
There is only one way to achieve the result 8. That is if both dice gives a four (4,4)
The same goes for the result 2 (1,1).

But the center value 5 can be achieved in 4 different ways: (2,3) (3,2) (1,4) (4,1)

Thus it is 4 times more likely to give the result 5 than e.g. 8.

Re: Dice Roller Example

PostPosted: Sat May 05, 2018 12:32 am
by ElliotM
Thanks for taking the time to explain that, that does make a lot more sense.

Re: Dice Roller Example

PostPosted: Sat May 05, 2018 2:45 pm
by ralphmerridew
Grammar pedant mode: 2d4 is four times as likely to give the result 5 as it is to give 8. It is three times more likely to give 5 as it is to give 8.

Denk's version does appear to give correctly biased results. I had it roll 3d6 a total of 3780 times, and the results were reasonably close to what should be expected.

Also, because you're using recursion, you have to watch out for stack overflow. This occurred between 250-260 dice for me.

Because ADRIFT doesn't use short-circuiting IF expressions, I don't think it's possible to write it as a user-defined function.

Re: Dice Roller Example

PostPosted: Sun May 06, 2018 12:03 am
by Denk
ralphmerridew wrote:Grammar pedant mode: 2d4 is four times as likely to give the result 5 as it is to give 8. It is three times more likely to give 5 as it is to give 8.
Oops- you are right :)
ralphmerridew wrote:Also, because you're using recursion, you have to watch out for stack overflow. This occurred between 250-260 dice for me.
Good point - I did not think of that problem.

Re: Dice Roller Example

PostPosted: Sun May 06, 2018 6:04 am
by Lazzah
Does anyone have a clue what these guys are talking about? :? :haha:

Re: Dice Roller Example

PostPosted: Sun May 06, 2018 7:08 am
by saabie
A D4 is a 4-sided die. Normal dice have 6 sides and so would be called D6.
The 2 in front of 2D4 just means you roll 2 of these dice to get a total between 2 and 8.
D4.jpg
D4.jpg (4.83 KiB) Viewed 1951 times

Re: Dice Roller Example

PostPosted: Sun May 06, 2018 7:43 am
by Lazzah
Thank you for your ever-informative reply saabi! :yeah:

It's a beautiful morning here in London, the sun is shining through the window and I suppose I should think about getting up out of bed! :whistle: 8) :wink: