- Joined
- Oct 5, 2016

- Messages
- 9

- Reaction score
- 6

- First Language
- English

- Primarily Uses
- RMMV

I've dipped here and there at times to check on plugins and sometimes to ask questions but I felt I needed to at least contribute a little as well. Hence, while my scripting / coding has been rusty due to being out of practice, it also gave me an opportunity to dive right back into it as well.

**INTRODUCTION**

So first off, I've often made searches for a simple plugin to emulate dice rolls and it is possible I've just been unlucky but I never did come across one. So I figured I might as well make my own and after testing it, it seems to be working pretty good for the needs I had of it.

But then, I hear you say "Why not just use a Math.random() function to generate a random value between X and Y? My simple answer is "because of odds".

For let's say that you want to emulate a classic 3d6 rolls for stats in a D&D game, the Math.random() method would allow you to generate a value between 3 and 18 with ease... however, that method would result in each possible value having the same odd, which is 1 in 16. This will possibly satisfy most people as you then have a desired result between 3 and 18.

However, with a proper dice roll, the odds of getting the most extreme values on a roll are FAR lower than say getting a 10 or 11 simply because of the distribution of results from each possible outcome of each die. For example, landing a 3 or 18 with an actual dice roll? You can only achieve it if you roll all 1s or all 6s (which is about 0.4%)... but to get 10? You have a lot more combinations possible that will result in 10 ([6,3,1], [6,2,2], [6,1,3], [5,3,2], etc.) meaning that the results will more often than not be near 10-11 (about 12.5%).

Hence the need for a proper dice roller function to emulate an actual roll of dice.

**HOW IT WORKS**

So as you might imagine, making a dice roll function isn't that difficult... it just needs to be made really to be usable and be called when needed with a few features to make it easier to use.

Basically, I've created the diceroll() function which, when called, requires 2 parameters : the number of die and the type of die. Thus, if you wanted to perform the aforementionned "3d6", you call a script function and enter :

JavaScript:

`diceroll(3,6);`

The function will then perform 3 consecutive Math.random() functions where a value between 1 and 6 will be generated then added to their previous result, returning the sum of all three. This will effectively and properly generate a dice roll with proper odds associated with that kind of roll.

Of course, since it is a custom function, it means that you can call it in your damage formula for skills and even use battler parameters as the parameters to send to the function to generate those rolls. So you could very well end up creating some sort of "D&D-like" weapon attack roll like so :

JavaScript:

`diceroll(2,4) + Math.Int((a.atk - 10) / 2)`

.. which would be the same (somewhat) to using a 2d4 damage weapon and adding your character's STR (ATK?) mod to the damage itself.

But then, what if you wanted to make a standalone roll and use it in your game later on like, let's say, a skill check of some sort? Well I've added a plugin parameter to let you drop the result of your roll into any one game variable you want.

As long as the plugin parameter "DiceRollVar" value is 1 or greater, then the result of your rolls will ALWAYS be copied into that game system variable so you can reserve and name a variable in your game to receive those precious dice. If the value is 0 or less then it will simply ignore that step entirely.

**GIMME!**

Here's the code in its entirety :

JavaScript:

```
//=============================================================================
// WIEG_DiceRoller.js
//=============================================================================
/*:
*
* @author Wiegraf
* @plugindesc Creates a function that replicates dice rolling.
*
* @param DiceRollVar
* @desc Variable ID where the result is copied to (none if 0)
* @default 0
*
* @help
* | Information |
* This simple plugin's purpose is to simulate actual dice rolling probabilities
* instead of relying on the usual method of using a random number generator
* range to obtain a similar result.
*
* The difference lies in the probability of each value occuring when generating
* such ranges. For example, taking the example of wanting to simulate a roll of
* 2 six-sided dice (which would result in a value between 2 and 12).
*
* Using the Math.rand() function to generate a value between 2 and 12 would mean
* that each of the 11 possible values would have 1 in 11 chance (or 9.0909%) of
* occuring by using "Math.rand() * 11 + 2".
*
* However, with a roll of 2 six-sided dice (or 2d6), the probability that you
* would roll either a result of 2 or 12 are both 1 in 36 (or 2.7777%) while
* rolling a result of 7 would be 6 in 36 (or 16.6667%) due * to the result being
* generated by 2 random checks of ranges of 1 to 6 instead of a single check of a
* range of 2 to 12.
*
* | Configuration |
* The plugin has a single configurable parameter named "DiceRollVar" which
* determines into which variable * ID the result of the dice roll will be copied.
* By default, the value is set to "0" which ignores that step, meaning the result
* isn't copied to a variable. If set to any value greater than 0 however, the result
* will be copied into the matching variable with the ID of the value entered.
*
* This allows the user to simply use the diceroll() function to quickly generate
* a number that can then be accessed through the ingame variables.
*
* | Usage |
* You can execute the diceroll function though an event with the "Script" advanced
* command.
*
* The "diceroll()" function requires 2 values to be entered in order to function :
* a number of die to be rolled and the type of die. Both values must be positive
* integers (aka rounded numbers). The proper syntax is as follows :
*
* diceroll(number,type) -> Roll "number" die with "type" faces
* diceroll(2,6) -> Roll 2 dice with 6 faces for a total between 2 and 12
* diceroll(4,8) -> Roll 4 dice with 8 faces for a total between 4 and 32
*
* Likewise, it is possible to use the diceroll() function in your damage formulas
* as long as you provide * integer-type values as the "number" and "type". For example :
*
* diceroll(a.atk, 4) -> Roll "a.atk" dice with 4 faces
* diceroll(2,6) + a.atk -> Roll 2 dice with 6 faces for a total between 2 and 12
* plus the user's Attack
*
*/
(function() {
//Registers the Plugin for use
var parameters = PluginManager.parameters("WIEG_DiceRoller");
//A place that holds all the parameters from your plugin params above
const WIEG_AdvancedSettingsParams = {
DiceRollVar: Number(parameters['DiceRollVar'])
};
diceroll = function(dieNumb, dieType) {
var i;
var total = 0;
for (i = 0; i < dieNumb; i++) {
total += Math.floor(Math.random() * dieType + 1);
}
if(WIEG_AdvancedSettingsParams.DiceRollVar > 0) {
$gameVariables.setValue(WIEG_AdvancedSettingsParams.DiceRollVar, total);
}
return total;
};
})();
```

**COPYRIGHT AND STUFF**

I only ask that you credit me if you use that plugin in your game.

**END WORD**

If you ever have questions or suggestions of features to add to this plugin, let me know and I'll consider it.