Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

randomInteger() method failing to roll proper minimum roll

1400963535

Edited 1400964008
var maxweapondam = parseInt(getAttrByName(who.id, 'damage', 'max') * 2); var minweapondam = parseInt(getAttrByName(who.id, 'damage')); var damageroll = randomInteger(maxweapondam) + minweapondam; if (damageroll > maxweapondam) { damageroll = maxweapondam; } Attribute damage has a value of 2 and max of 6. This is to simulate damage from a weapon that does 2d6 worth of damage. So the minimum value that should be returned from this simulated dice roll should be 2 and the max 12. While the maxdamage is never over 12, I can not get the minimum damage to be 2. I am logging damageroll and the lowest number in the series is 3, over a good 30 rolls. when I log minweapondam I am returned 2. I know randomInteger(max) can return 0 that's is what the minweapondamage is set to the number of dice as the lowest each can roll is 1. 1+1=2 so why does this never produce minweapondam? Edit: after further testing I have concluded that randomInteger(max) will return a value of 1 through max. So I guess I need to add and if statement after to check if the return was a 1, and if it was, set damageroll to equal minweapondam. Edit 2: Yep, works like a charm now.
1400964351

Edited 1400965633
Here is what the script function now looks like: //Get min and max weapon damage values. //Min value is the number of dice rolled and max is the number of faces on the dice var minweapondam = parseInt(getAttrByName(who.id, 'damage')); var maxweapondam = parseInt(getAttrByName(who.id, 'damage', 'max') * minweapondam); //Simulate a damage roll var damageroll = randomInteger(maxweapondam); if (damageroll == 1) //If the roll was 1 make it the minimumvalue { damageroll = minweapondam; }
1400964901

Edited 1400965010
Lithl
Pro
Sheet Author
API Scripter
Michael P. said: var maxweapondam = parseInt(getAttrByName(who.id, 'damage', 'max') * 2); var minweapondam = parseInt(getAttrByName(who.id, 'damage')); var damageroll = randomInteger(maxweapondam) + minweapondam; if (damageroll > maxweapondam) { damageroll = maxweapondam; } Attribute damage has a value of 2 and max of 6. This is to simulate damage from a weapon that does 2d6 worth of damage. So the minimum value that should be returned from this simulated dice roll should be 2 and the max 12. While the maxdamage is never over 12, I can not get the minimum damage to be 2. I am logging damageroll and the lowest number in the series is 3, over a good 30 rolls. when I log minweapondam I am returned 2. I know randomInteger(max) can return 0 that's is what the minweapondamage is set to the number of dice as the lowest each can roll is 1. 1+1=2 so why does this never produce minweapondam? randomInteger() produces a minimum value of 1, not 0. The function is Roll20-specific, and it for simulating dice rolls (and dice have a minimum value of 1, for the vast majority of dice in the world). The JavaScript Math.random() function is available to you, and has a minimum value of 0, but the JS random algorithm isn't as sophisticated as the one used by randomInteger. In short, randomInteger(x) produces an integer in the range [1..x]. random() produces [0..1).
Yea, I see that. I have updated my function to reflect this correction of my ignorance.