# How can I calculate the distribution of “Roll 3D6, reroll any dice once, treat all 1's as a 2” in AnyDice?

I'm wondering how to use Anydice to calculate the following:
Roll 3d6, treating all 1's as 2's, you may reroll each die once, so we do this if the roll is below the average.

So 3d6, can reroll each dice once, and we'll reroll on 2 and 3 because its below average.

I've given it a go, but I'm not sure if I got it right

Here is my anydice attempt

The first step to make it easier to recognise that our dice are independent of each other meaning we can figure out how one of them works and then add them together.

Firstly, treating all 1s as 2s is actually pretty easy if you know how to define custom dice:

D: {2, 2..6}


Which is a d6 but with the 1 replaced with a 2. (This is technically a sequence which is why we use 1dD to roll it later.)

We then need to work our how to do a singular reroll for which we can use a custom function. Note that it is not equivalent to taking the higher of two rolls as you keep 4s and 5s even if the second (then unrolled roll) would be higher.

function: reroll X:n {
if X <= 3 {
result: 1dD
}
else { result: X}
}


This function takes in a die and converts it to a number, which means it will be iterated for each possible result (make sure not to pass a sequence as that will be converted to the sum). We then check if the 'roll' is 3 or less and if it is we return a new roll (the reroll), otherwise we just return the value roll.

To get the output for 3 such rolls we just call:

output 3d[reroll 1dD]


Which gives the following distribution:

You can check out my anydice program for this here (includes some outputs for singular rolls as a sanity check).

• A small thing perhaps worth mentioning, you do have to recalculate the die's average to determine when you would actually reroll it. It just so happens that the current die goes from the typical average of 3.5 to only 3.666 so you still reroll on the same values. This would not be the case if, say, 1's were treated as 6's – Medix2 Apr 6 '20 at 16:32

Here's another solution that's fairly easy to generalize. It's based on this helper function:

function: ROLL:n replace FILTER:s with REROLL:d {
if ROLL = FILTER { result: REROLL }
result: ROLL
}


Using this function, we can generate a bunch of custom dice:

X: [d6 replace 1 with 2]
Y: [d6 replace {1..3} with d6]
Z: [dY replace 1 with 2]


…and roll three of each of them:

output 3d6 named "3d6"
output 3dX named "3d6, count 1 as 2"
output 3dY named "3d6, reroll 1-3 once"
output 3dZ named "3d6, reroll 1-3 once, count 1 as 2"


The output, graphed, looks like this:

(Note that it doesn't matter in this case whether we first reroll the d6, and then replace 1s with 2s, or vice versa — either way, both natural 1s and 2s get rerolled once and then counted as 2. I could've just as well written Z: [dX replace {2..3} with dX] and gotten the exact same results. More generally, e.g. if you wanted to count 1s as 2s only on the first roll, it could matter. Also, as Medix2 points out in the comments, in general replacing some rolled numbers with others could change which numbers are worth rerolling. In this case, however, it doesn't.)