Title: Dice Rolling Plugin
Last modified: August 18, 2016

---

# Dice Rolling Plugin

 *  [Anki](https://wordpress.org/support/users/anki/)
 * (@anki)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/)
 * I was wondering if anyone knew of or could make a dice rolling plugin.
 * I want to have Dungeons and Dragons (or any d20 game) campaigns running on my
   wordpress and I just need a dice rolling script.
 * Thanks.
 * ~ anki

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Lorelle](https://wordpress.org/support/users/lorelle/)
 * (@lorelle)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/#post-262563)
 * Check any of the PHP or Javascript script places to see if they have something
   like that. I’ve not seen a specific WordPress plugin, but you can create a template
   [Page](http://codex.wordpress.org/Pages) on your WordPress site that has a plugin
   in the template file that will generate any PHP script that will do that. I’m
   sure there must be something like that as many students do random scripts which
   return random numbers as part of their studies.
 *  [viper007bond](https://wordpress.org/support/users/viper007bond/)
 * (@viper007bond)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/#post-262578)
 * `<?php echo mt_rand(1, 6); ?>`
 *  Thread Starter [Anki](https://wordpress.org/support/users/anki/)
 * (@anki)
 * [20 years, 8 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/#post-262579)
 * Well, I need it like where they call the command suchas [dice]d20[/dice] or :
   dice d20: Also it needs modifiers (addition and subtraction) Theres, d4 d6 d8
   d10 d12 d20 and d100 and then also multiple dice as well.
 *  [joshualayne](https://wordpress.org/support/users/joshualayne/)
 * (@joshualayne)
 * [20 years, 7 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/#post-263129)
 * this should work:
    ` <?php function twentyRolls($type, $modifier=0){ $i=1; while(
   $i <= 20){ $output = mt_rand(1, $type) + $modifier; echo $i . ': ' . $output .'
   <br/>'; $i++; } } twentyRolls($_GET['d'], $_GET['mod']); echo '<br/><br/>'; function
   dice($type, $modifier=0){ $output = mt_rand(1, $type) + $modifier; if ($modifier
   < 0){ $modText = $modifier; } else { $modText = '+' . $modifier; } echo 'Die 
   roll (d' . $type . ' ' . $modText . '): ' . $output . '<br/>'; } dice($_GET['
   d'], $_GET['mod']); ?>
 *  [joshualayne](https://wordpress.org/support/users/joshualayne/)
 * (@joshualayne)
 * [20 years, 7 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/#post-263130)
 * in the post above, “twentyRolls” is just to give an idea of the variability.
 * the function you would want would be dice();
 * obviously, you could change the `$_GET['variable']` to whatever you needed them
   to be, e.g.:
    dice(20, +5); //d20+5
 * (equivalent to dice(20, 5);)
 * of course, rereading your post, I see the requirement for multiple dice…
 * here you go (bored and avioding work)
    ` <?php function dice($type, $modifier
   =0, $num=1){ $i=0; while ($i<$num){ $roll[$i] = mt_rand(1, $type) + $modifier;
   $i++; } $total=0; foreach($roll as $value){ $total = $total + $value; } return
   $total; } echo(dice($_GET['d'], $_GET['mod'], $_GET['num'])); ?>
 * HTH,
    j.
 *  [vkaryl](https://wordpress.org/support/users/vkaryl/)
 * (@vkaryl)
 * [20 years, 7 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/#post-263131)
 * Whoa – pretty clever guys! Now THIS is something cool and fun to do with a blog….
   as much fun in a way as running tabletop games for my daughter and her friends
   30 years ago….
 *  [joshualayne](https://wordpress.org/support/users/joshualayne/)
 * (@joshualayne)
 * [20 years, 7 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/#post-263132)
 * 😛
 * until I get banned for using up bandwidth with useless code….
 * (I actually have gotten some out-of-bounds values using this, so it is probably
   buggy – use in a campaign at your own risk…. beer drinking, club wielding barbarians
   might not think it’s funny to get a 2 when rolling 3d6 (with no modifers)…
 * more user friendly:
    `<?php // of the form 3d6+2 function dice($arg){ //var_dump(
   $arg); $splitArg1 = preg_split("/d/", $arg); $num = $splitArg1[0]; if(strstr(
   $splitArg1[1], '+')){ $splitArg2 = preg_split("/+/", $splitArg1[1]); $dice = 
   $splitArg2[0]; $mod = $splitArg2[1]; } else if (strstr($splitArg1[1], '-')){ 
   $splitArg2 = preg_split("/-/", $splitArg1[1]); $dice = $splitArg2[0]; $mod = -
   $splitArg2[1]; } else { $dice = $splitArg1[1]; $mod = 0; } $i=0; while ($i<$num){
   $roll[$i] = mt_rand(1, $dice); $i++; } $total=0; foreach($roll as $value){ $total
   = $total + $value; } $total = $total + $mod; return $total; } echo(dice('3d6+
   4')); ?>
 *  Thread Starter [Anki](https://wordpress.org/support/users/anki/)
 * (@anki)
 * [20 years, 7 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/#post-263138)
 * Thanks guys for all your help. I’ll let ya’ll know if any of them work.
 * Also, where would I put the code at?

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Dice Rolling Plugin’ is closed to new replies.

 * 8 replies
 * 5 participants
 * Last reply from: [Anki](https://wordpress.org/support/users/anki/)
 * Last activity: [20 years, 7 months ago](https://wordpress.org/support/topic/dice-rolling-plugin/#post-263138)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
