Javascript Dice probability calculation with rerolls and custom dice

If you don't want to calculate dice roll probabilities you won't want to read this. is good for simple things, but I don't understand how to calculate probabilities for "If the lowest die is a 3 then roll an extra die unless.." rules that you sometime have in board games.

Here is a Javascript program that rolls dice with any number of sides, any number on each face of the dice, and any kind of rules you want to implement. Here I use a "If either die is Zero, re-roll one die that is zero" function, it isn't difficult to  program different rules.

The program just makes lots of dice rolls and takes an average.

red = [1, 2, 3, 0, 0, 0], //Red Space Crusade die
white = [1, 2, 0, 0, 0, 0], //White Space Crusade die
Here is a simple Javascript program that rolls:
 2 white Space Crusade dice (see above picture), rerolling one zero value.
 2 red Space Crusade dice, rerolling one zero value.
 2 D6 dice, simple total.

For example, this function rolls 2 dice and returns the total. The dice can have any number of sides, and any number on each side. You can use if/then/else statements to handle things like doubles or snake eyes.

        function rollTwo(colorDie) {//simple total
            var die1, die2;
            die1 = rollDie(colorDie);
            die2 = rollDie(colorDie);
            return die1 + die2;
The "trials" function takes the dice, number of rolls, and the function to apply:
         function trials(diceArray, times, funct) {
            var i, roll, result = [];
            for (i = 0; i < times; i++) {
               roll = funct(diceArray);
               while (result.length < roll + 1) {
               result[roll] = result[roll] + 1;
            return result;
The "displayResult" function just creates a display string from the contents of the "result" array.
There is a paragraph in the .html to display the result string.
That's about it.

Sample output:
The rolls were
0: 29.34 %
1: 22.1 %
2: 29.05 %
3: 12.61 %
4: 6.9 %
The rolls were
0: 12.72 %
1: 13.07 %
2: 17.71 %
3: 23.4 %
4: 16.35 %
5: 11.31 %
6: 5.44 %
The rolls were
0: 0 %
1: 0 %
2: 2.75 %
3: 5.64 %
4: 8.24 %
5: 11.06 %
6: 13.89 %
7: 16.21 %
8: 13.78 %
9: 11.36 %
10: 8.79 %
11: 5.51 %
12: 2.77 %

The chart gives the same numbers as the text output above it, 7 is 16.21 % (16.67 % is the correct answer) for example.
The program rolls the dice 10,000 times, so you never get exactly the same numbers. 
If you copy and paste the text below into a text editor and save it as "whatever.html", you can click on the file and it runs in the browser.

<===================== below this line is the program ========================>

<!DOCTYPE html>

      <p id="white"></p>
      <p id="red"></p>
      <p id="d6"></p>       

         var white = [0, 0, 0, 0, 1, 2], //White Space Crusade die
                 red = [0, 0, 0, 3, 1, 2], //Red Space Crusade die
                 d6 = [1, 2, 3, 4, 5, 6], //Normal D6 
                 d10 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ], //Normal D10
         document.getElementById("red").innerHTML =
         document.getElementById("white").innerHTML =
         document.getElementById("d6").innerHTML =
         result = trials(white, 10000, rerollOne);
         document.getElementById("white").innerHTML =
                 "The rolls were <br>" + displayResult(result, 100) + " ";
         result = trials(red, 10000, rerollOne);
         document.getElementById("red").innerHTML =
                 "The rolls were <br>" + displayResult(result, 100) + " ";
         result = trials(d6, 10000, rollTwo);
         document.getElementById("d6").innerHTML =
                 "The rolls were <br>" + displayResult(result, 100) + " ";

         function displayResult(anArray, divide) {//result array, divisor for output to make it look like a percentage
            var i, limit, str = "";
            limit = anArray.length;
            for (i = 0; i < limit; i++) {
               str += i;
               str += ": ";
               str += anArray[i] / divide;
               str += " %<br>";
            return str;

         function trials(diceArray, times, funct) {//die array, number of iterations, function to use
            var i, roll, result = [];
            for (i = 0; i < times; i++) {
               roll = funct(diceArray);
               while (result.length < roll + 1) {//make result array bigger if needs be
               result[roll] = result[roll] + 1;
            return result;

         function rollDie(colorDie) {//returns a random die roll result
            return colorDie[randIndex(colorDie.length)]
         function randIndex(num) {
            return Math.floor((Math.random() * num));
         function rollTwo(colorDie) {//simple total
            var die1, die2;
            die1 = rollDie(colorDie);
            die2 = rollDie(colorDie);
            return die1 + die2;
         function rerollOne(colorDie) {// if one result is zero, reroll it
            var die1, die2, reroll;
            die1 = rollDie(colorDie);
            die2 = rollDie(colorDie);
            reroll = rollDie(colorDie);
            if (die1 === 0) {
               die1 = reroll; //reroll first die if zero
            } else {
               if (die2 === 0) {
                  die2 = reroll; //otherwise reroll second die if zero
            return die1 + die2; //total of 2 dice


Space Crusade - "EXplore, EXpand, EXploit and EXterminate"

A lot of people complain that Space Crusade isn't enough like Civilization, or even Starcraft. That you don't get to "EXplore, EXpand, EXploit and EXterminate", just EXterminate! EXterminate! EXterminate! EXterminate!

TLDR: 12 cards to give you tech research centers to find and capture in Space Crusade.

Why don't the Marines just nuke the ship? Because it is full of lost technology the Imperium desperately needs. We must to find this tech, research it and use it to expand.

So let's explore the ship, find resources, research tech, and so on:
Each Board has a Research Center with special graphics "A", "B", "C" and "D".

Make 12 Tech cards (see below), just write the text on an index card. You don't have to use them all, you can make up new ones. C'mon, 10 minutes work for a new game.

The first time a miniature (not a blip) appears in a Research Center, place a random Tech Card in the room and reveal it. That room controls that one technology for the rest of the game.

A Player captures a Research Center as soon as they have a miniature in the room, with no other player's miniatures in the room. They hold the room (even if it is empty) as long as no-one else captures it. You can place an unused weapon or order card in the room to mark it as yours. If you hold the Center at the start of 3 consecutive turns (Rotate the card 90° at the start of each turn) you have researched a Technology. You may use that Technology only as long as you control the Center, although "Permanent" modifications (Psycho-Surgery, Genestealer) persist if the Center is lost.

12 Tech Cards:
  • Apothecary: Every time one of your miniatures dies (Not Androids), roll a white die. If the result is 1 or 2, the miniature's life is saved. The miniature cannot move or attack in its next turn, but if it wins in defending Hand to Hand, that counts as a draw.
  • Hydrogen Weapon Prototype: Your Marines, Chaos Marines and Commanders get the option of shooting any target with 2 red die instead of their normal attack. If the target takes no damage, the shooter suffers a Missile Launcher attack on his own square (39% chance of killing a Marine, 20% chance of killing yourself. Better than Hand to Hand, great against Orcs). 
  • Forceshield: Pick only one non-Commander Marine. After he spends a turn in the Forceshield Center, he gets armor of 3, and 2 red dice in hand-to-hand. The Forceshield bonus is lost if the Center is lost. If he is killed, a new Marine can visit the Center for a turn to get a new Forceshield.
  • Smoke Grenades: Instead of a normal attack, any non-Commander Marine or Chaos Marine can place a marker on a square in his line of sight. That square, and the 8 squares around it become Smoke squares until the start of your next turn, when the marker is removed. Nothing entirely in the center Smoke square can shoot or be attacked by Miniatures or Alien Event cards. All 9 Smoke squares block LOS and Plasma fire.
  • Meth Lab: All your miniatures get to move 2 squares further each turn, to a limit of 8 squares.
  • Ship Systems : At the start of your turn you may open or close any 2 of the individual doors.
  • Sacred Artifact: If you have a Marine Commander or Chaos Commander, that Commander gets an attack method with psychic energy. They can attack any miniature within 6 squares (diagonally OK) with 1 red dice and 1 white dice combined, Line of Sight is not required.
  • Psycho-Surgery: Any 1 miniature (not Androids) in this Center at the start of your turn can be Permanently converted to a Berserker. The surgery takes 1 complete turn, the patient may neither move nor fire. The Berserker rolls an extra red die in Hand-to-Hand, but must always move towards the nearest enemy by the most direct route at the start of its owners turn. If the Center is recaptured, the miniature keeps its Berserk status.
  • Transporter: Roll a red die to lock on to any miniature (Friend or Foe) on the board at the end of your turn. If the result is 1-3, beam the miniature to any unoccupied square on the same board at the start of your turn.
  • Sentry Gun Center: At the start of your turn, attack one miniature on any corridor square on the same board as the Center with a total of 1 red die and 1 white die.
  • Power Armor Upgrade: All your Marines or Chaos Marines your Marines get improve armor control software, they can fight multiple opponents. If they win in Hand-to-Hand, all enemies adjacent to the Marine die. Heavy weapons have a move of 5.
  • Self Destruct Center: Announce 1 target board (from the 4), and "Self Destruct Sequence Initiated 4" at the start of your turn. At the start of your next turn announce "Self Destruct Sequence Initiated 3", and so on.  When you announce "Self Destruct Sequence Initiated 0", every miniature on the target board is immediately killed. Once started, the sequence is aborted only when the Self Destruct Center changes hands. (Everyone on the target board will have to leave by 1 of 2 exits, where ambushes may await. The Center on the abandoned sector is open to capture.)
So there you have it, the Player who holds key terrain will tech up and dominate the game.

The Alien starts a Self Destruct sequence on the last board it occupies. "To the last, I grapple with thee; From Hell's heart, I stab at thee; For hate's sake, I spit my last breath at thee". Will the Marines try for a transporter lock on the Dreadnought in the Self Destruct Controller Room, or will the Marines flee for the Docking Claw? You decide!

Make up some cards of your own. Plague, Nothing, Skull Probe, whatever.

Space Crusade Dice Rolls

Here are dice probabilities for Space Crusade. You have a 38.8888889% chance of rolling a 3 or more with 2 red dice, and an 8.33333333% with of rolling a 3 with 2 white dice. So a Chaos Commander is 4.7 times more likely to kill a Marine than a Bolter Chaos Marine when shooting.

2 white 2 white, 1 reroll
Score % Score %
0 100 0 100
1 55.55556 1 70.4
2 33.33333 2 48.3
3 8.333333 3 19.5
4 2.777778 4 6.5
2 red 1 red
Score % Score %
0 100 0 100
1 75 1 50
2 58.33333 2 33.33333
3 38.88889 3 16.66667
4 16.66667
5 8.333333
6 2.777778
2 red 2 white 1 red 1 white
Score % Score %
0 100 0 100
1 88.88889 1 66.66667
2 75.92593 2 47.22222
3 57.33025 3 25
4 37.57716 4 8.333333
5 22.4537 5 2.777778
6 11.03395 1 white
7 4.70679 Score %
8 1.62037 0 100
9 0.385802 1 33.33333
10 0.07716 2 16.66667
3 white 3 red
Score % Score %
0 100 0 100
1 70.37037 1 87.5
2 48.14815 2 75
3 20.37037 3 58.33333
4 8.796296 4 37.03704
5 1.851852 5 23.14815
6 0.462963 6 12.03704
7 4.62963
8 1.851852
9 0.462963

The reroll almost doubles the bolter's odds against Androids and Chaos Marines. Although 2 red die are still 4.7 times better.

Space Crusade - Techmarines

Techmarines repair and maintain equipment. They belong to the Cult Mechanicus and to their Chapter.
”It’s like there's a party on my back and everyone’s getting killed!”.
They are bolter Marines with Robot arms and cutting tools. They can fix malfunctions. So Space Crusade rules:

Move 6:
Armor: 2 
Shooting: 2 White
Hand-to-Hand: 2 Red, 2 extra White against Androids, Dreadnoughts and Bulkheads.
Cost: 1 Equipment card.
Point cost 5.

He can take a Heavy Weapon, he becomes a Heavy Weapon Marine, reducing his speed to 4, and his point cost goes up to 10.

Alien Event:
"Equipment Malfunction": If the Techmarine does not move or fire for a whole turn, he will undo the card on a Red die roll 1-3 if he is adjacent to the weapon for a whole turn, or if he is carrying the weapon himself. He may try as many times as he likes.

Comment: A Space Hulk would have mechanical issues, so it makes sense bring a Techmarine aboard. 2 red dice have a 1:12 chance of damaging a Dreadnought 2 red>4, but a Techmarine can hand-to-hand Dreadnought on even terms (each has a 42.81% of a kill per attack 2 red+2 white > 2 red+2 white ). A Meltabomb has 55.64% chance of beating the Dreadnought in Hand-to-Hand per turn.
He makes a more reliable Heavy Weapons marine, the Alien will jam the weapons of the other Chapter. But he can't outrun a Dreadnought with with a move of 4.

Space Crusade Apothecary - Shoot the Medic First

It's worse than that, he's dead Jim!

The Apothecary's job is to tend to the wounded, and to milk the gene seed out of dying Marine's Progenoid glands. Nice work if you can get it.
The Apothecary costs 1 equipment card, otherwise he is a normal Bolter Marine (If he has other weapons, work out some reasonable attack dice values).

Apothecary Rules:
If a Marine is killed within 1 square (diagonally OK) of an Apothecary, roll a die. If the result is a 1, the Marine's life is saved. Otherwise the Marine dies, take the dead miniature and place it on the Marine Control Panel (the Apothecary has the gene seed).  The Alien player gets no points for that kill. If the Apothecary is killed, give any miniatures on the Marine Control Panel to the Alien, they now get the points. The Apothecary cannot heal himself.

You will presumably keep your Apothecary beside your heavy weapons. He will either save the heavy weapon gene seed, or draw fire that would otherwise be directed at the heavy weapons. Naturally the Alien will try to kill your Apothecary with Encounter cards if he is loaded up with Gene Seed. He may not be the best use of an equipment card, but if he doesn't get killed he has a good chance of saving a marine and stealing 10 points from the Alien.

Space Crusade Simple Librarian Rules

The Librarian is a non-commander Marine, replacing one normal Marine per squad.
In the grim darkness of the far future, there is no backlit Kindle.

Stats and Weapons:
The Psychic miniature will have a big sword or stick, and maybe a gun, so we can arm him like a Commander with either bolt pistol + power axe, or power glove + power sword. He does not count as a heavy weapon carrier, his movement is 6, with 1 life point. He is worth 12 points to the Alien.

Psychic Abilities:
The Librarian can take control of any 2 Alien events, attacking the Alien instead of the Marines, at the cost of one Equipment card. Or 4 Alien events at the cost of 2 Equipment cards.

Comment: the Librarian gives the Marines great Hand-to-Hand capability, and he can turn Genestealers and booby traps against the enemy.

Terminator Librarian:
(Based on The Terminator Librarian in White Dwarf 134 with a Power Axe) armor is 3, 1 red + 1 white dice for firing, 2 red die + 2 white dice for hand to hand. Move 4 squares. Costs 2 Equipment Cards, worth 12 points to the Alien.

If there is only 1 Marine player, they may a exchange a Librarian for one Marine, he can can take control of any 2 Alien events without costing 1 equipment card. He is worth 5 points to the Alien.

Space Crusade Conversion Beamer and Las-Cannon

Mission Dreadnought adds "Extra-Heavy Weapons": the Conversion Beamer Las-Cannon and Fusion Gun. These are deadlier than the normal heavy weapons, but they don't get Order or Equipment card bonuses. The Aliens get 10 points for each extra-heavy weapon Marine killed.

Conversion Beamer:
Conversion Beamer
The conversion beamer has to be fired at a wall or a closed door. I does not require line of sight to the wall or door.  The damage can affect the firer if he is close enough, making it difficult to get into position. You roll 3 red dice, the example show what happens if your total is 6.
A Psilencer, but I pretend it is a Conversion Beamer

The Las-Cannon has 3 red dice, you can make 3 attacks with 1 die each, 1 attack with 3, or 2 attacks, 1 with 2 and the other with 1. Each attach affects a 2x2 square. Below, the marine rolls 2 dice against the aliens on the left, and 1 die against the aliens above. You don't seem to need line of sight on the far corner.


Fusion Gun:
There is also the Fusion Gun, which is a Plasma Gun that rolls 3 red dice, but you roll separately for each target. Not very imaginative.

There is also the Tarantula, which is a miniature operated by a separate marine. It can't move and fire in the same turn. It is a Las-Cannon that rolls 3 red dice, has a 90° firing arc, and a move of six when adjacent to a Marine Tarantula Gunner. The gunner has an armor value of 4 from the front arc, so it can fight a Dreadnought face to face, although the Alien will attack it from behind with an Orc instead.
Arc of fire
You can pretend that the Plasma gun is a Conversion Beamer. Or use the EM4 Space Rangers 1=Fusion, 2=conversion, 3=Las-Cannon:
The EM 4 Mechs have a dual Las-Cannon, you could glue it to a base and call it a Tarantula. The EM4 minis are really cheap. They also have a rocket pod, so you could have a "Black Widow" which is a Tarantula that works like a rocket launcher, except with 3 red die.

Pwork Starbase Bundle for Space Crusade

Update: At Staples, the tiles do not print as 1" squares, I have printed the ,pdf's to .pdf at 113% scale, perhaps that will work.

I wanted some flexible tiles for Space Crusade, and Pwork Starbase had a set of tiles similar to Space Hulk, but with 1" squares and 2 squares wide corridors. Hilariously, the Pwork website wanted me to pay for shipping on a download that they couldn't even spell the name of:
so I bought it at DriveThruRPG. Normally DriveThruRPG has a 2" square graphic, and you don't know what you are getting. Pwork is really weird in the sense that they actually have a picture of the product that lets you see what it is:
 Just what you would expect a modular Space Crusade to look like.
However, there are no jigsaw ends, so everything will slip and slide. I bought "200pcs 2mm x 1mm Disc Rare Earth 2x1mm Super Happy Lucky strong Magnets" for US $2.84 on ebay. I'll print on thin paper, glue it on to foamboard and glue in rare earth magnets. Although how rare can they be, at 1.42 cents each?
The magnets may or may not hold things together, but I don't care what I do, I live on the edge as Adrian Edmondson would say.

The pdfs look nice, although you would be in violation of all that is holy if you unlocked the .pdf, deleted the pages you don't want to print (like the instructions) and got it printed at Staples. So don't do that, Adobe will come out of the Warp to get you.

The point is that the corridors match the width of the Space Crusade corridors, so you can combine the original boards with Space Hulk style (Space Hulk squares are about 30% bigger than Space Crusade so the terminators can pose) extensions. $10 worth of printing and foamboard should do it, plus the magnets. With an Overwatch houserule, the long corridors can be covered with heavy weapons, creating tactical puzzles. Expansion 01 has pinched off 1 square corridor pieces:
This gives you Dreadnought free Zones, and a Space Hulk claustrophobia, where 1 guy can seal off a corridor until he dies randomly. If you have Cover and Overwatch houserules, these would be game changers.
There is a Fantasy version too. Therefore the Aliens might attack the ancient inner sanctum of the Space Marines:
Also a City Ruins for 40K and such. You even use the Starbase tiles as Dungeon tiles for Heroquest variants. Pwork's "The Cave" looks a bit like Advanced Space Crusade, you could pretend it is an Alien Colon instead of a cave: