ADRIFT Forum


The place to discuss the ADRIFT Interactive Fiction toolkit

Dice Roller Example

This forum is the place to learn about and discuss ADRIFT 5. Feel free to mention any bugs you find here, but please also add these to the Bugs & Enhancements list.

Please also refer to the ADRIFT 5 Wiki for more information.

Dice Roller Example

Postby ElliotM » Tue May 01, 2018 7:30 pm

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 5 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.
ElliotM
 
Posts: 1227
Joined: Tue Apr 17, 2007 8:04 pm
Location: The Midwest in the USA
Points: 13

Re: Dice Roller Example

Postby Campbell » Wed May 02, 2018 1:50 pm

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.
ADRIFT Developer developer.
User avatar
Campbell
Site Admin
 
Posts: 4618
Joined: Sun Jun 23, 2002 11:05 am
Location: Edinburgh, Scotland

Re: Dice Roller Example

Postby ElliotM » Wed May 02, 2018 3:50 pm

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.
ElliotM
 
Posts: 1227
Joined: Tue Apr 17, 2007 8:04 pm
Location: The Midwest in the USA
Points: 13

Re: Dice Roller Example

Postby ralphmerridew » Wed May 02, 2018 9:37 pm

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.
Bloodhounds can make you laugh and cuss in the same breath. They are endearing, faithful, and can sling drool ten feet in any direction. -- Virginia Lanier
User avatar
ralphmerridew
 
Posts: 2533
Joined: Fri Dec 13, 2002 11:56 pm
Location: Missouri
Points: 10

Re: Dice Roller Example

Postby ElliotM » Wed May 02, 2018 11:24 pm

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)
ElliotM
 
Posts: 1227
Joined: Tue Apr 17, 2007 8:04 pm
Location: The Midwest in the USA
Points: 13

Re: Dice Roller Example

Postby Denk » Thu May 03, 2018 12:01 am

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.
Attachments
Dice Roller Modified.taf
(15.58 KiB) Downloaded 4 times
-----------------------------------------------------------------------------------------
Out now: The Royal Puzzle, The Way Home & The Dragon Diamond
-----------------------------------------------------------------------------------------
Denk
 
Posts: 103
Joined: Mon Feb 22, 2016 6:21 pm
Location: Hjørring, Denmark
Points: 11

Re: Dice Roller Example

Postby ElliotM » Thu May 03, 2018 12:37 am

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?
ElliotM
 
Posts: 1227
Joined: Tue Apr 17, 2007 8:04 pm
Location: The Midwest in the USA
Points: 13

Re: Dice Roller Example

Postby Denk » Fri May 04, 2018 10:38 pm

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.
-----------------------------------------------------------------------------------------
Out now: The Royal Puzzle, The Way Home & The Dragon Diamond
-----------------------------------------------------------------------------------------
Denk
 
Posts: 103
Joined: Mon Feb 22, 2016 6:21 pm
Location: Hjørring, Denmark
Points: 11

Re: Dice Roller Example

Postby ElliotM » Sat May 05, 2018 12:32 am

Thanks for taking the time to explain that, that does make a lot more sense.
ElliotM
 
Posts: 1227
Joined: Tue Apr 17, 2007 8:04 pm
Location: The Midwest in the USA
Points: 13

Re: Dice Roller Example

Postby ralphmerridew » Sat May 05, 2018 2:45 pm

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.
Bloodhounds can make you laugh and cuss in the same breath. They are endearing, faithful, and can sling drool ten feet in any direction. -- Virginia Lanier
User avatar
ralphmerridew
 
Posts: 2533
Joined: Fri Dec 13, 2002 11:56 pm
Location: Missouri
Points: 10

Re: Dice Roller Example

Postby Denk » Sun May 06, 2018 12:03 am

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.
-----------------------------------------------------------------------------------------
Out now: The Royal Puzzle, The Way Home & The Dragon Diamond
-----------------------------------------------------------------------------------------
Denk
 
Posts: 103
Joined: Mon Feb 22, 2016 6:21 pm
Location: Hjørring, Denmark
Points: 11

Re: Dice Roller Example

Postby Lazzah » Sun May 06, 2018 6:04 am

Does anyone have a clue what these guys are talking about? :? :haha:
OUT NOW: Run, Bronwynn, Run!
Current W.I.P.: Magnetic Moon
Also available: The Axe of Kolt, The Spectre of Castle Coris, The Fortress of Fear, Die Feuerfaust - The Fist of Fire and The Lost Children
User avatar
Lazzah
Moderator
 
Posts: 2104
Joined: Thu Mar 31, 2011 4:54 am
Location: London, England
Points: 17

Re: Dice Roller Example

Postby saabie » Sun May 06, 2018 7:08 am

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 152 times
saabie
 
Posts: 886
Joined: Fri Aug 12, 2011 2:07 am
Location: Adelaide, South Australia
Points: 10

Re: Dice Roller Example

Postby Lazzah » Sun May 06, 2018 7:43 am

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:
OUT NOW: Run, Bronwynn, Run!
Current W.I.P.: Magnetic Moon
Also available: The Axe of Kolt, The Spectre of Castle Coris, The Fortress of Fear, Die Feuerfaust - The Fist of Fire and The Lost Children
User avatar
Lazzah
Moderator
 
Posts: 2104
Joined: Thu Mar 31, 2011 4:54 am
Location: London, England
Points: 17


Return to ADRIFT 5.0

Who is online

Users browsing this forum: Google [Bot] and 4 guests

cron