How do I model the fighter’s Great Weapon Fighting fighting style in Anydice?


I was trying to create an AnyDice function to model the Great Weapon Fighting fighting style (which lets you reroll 1s and 2s), but I couldn’t get it to work on any arbitrary dice.

I’ve found this one:

function: reroll R:n under N:n {    if R < N { result: d12 } else {result: R} } output [reroll 1d12 under 3] named "greataxe weapon fighting" 

And it works fine. But I don’t know how to make the function generic so i don’t need to change the d12 every time i want a different dice to reroll.

I’ve tried

function: reroll R:n under N:n {    if R < N { result: d{1..R} } else {result: R} } output [reroll 1d12 under 3] named "greataxe weapon fighting" 

but it is not giving the right probabilities. Maybe if I could fetch the die size inside the function…