# Opportunity to write some idiomatic Julia code

**URL:** <https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702>\
**Category:** Community\
**Created:** [May 18, 2020, 4:14pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702 "2020-05-18T16:14:23Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![Ian\_Slagle](https://avatars.discourse-cdn.com/v4/letter/i/b5a626/32.png) [@Ian\_Slagle](https://discourse.julialang.org/u/Ian_Slagle)\
**Post date:** [May 18, 2020, 8:57pm UTC](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702/11 "2020-05-18T20:57:06Z")

</div>

I think I got the loopless version working. The mark probability can be seen as a function of the number of modifying steps. In this way, it is similar to the probability of getting two heads in a row after n throws of an unfair coin (in this case 1/3 heads, 2/3 tails). This probability I took from ([Two Heads in A Row: With Unequal Probability | ELEC424 Wiki | Fandom](https://elec424.fandom.com/wiki/Two_Heads_in_A_Row:_With_Unequal_Probability)), although there’s not a derivation so I’m not entirely sure it’s correct. Nonetheless, here’s my revised code.

```julia
using Distributions
D(x) = rand(1:x)
P(m) = m <= 1 ? 0 : P(m - 1) + (1 - P(m - 1)) / 3 - (1 - P(m - 2)) * (2 / 9)
function O(x) 
    roll, modifier = D(x), rand(Geometric(0.5))
    if (roll == x || roll == 1) && rand() <= P(modifier)
        println(roll == x ? "$(roll + modifier) critical" : "$(roll - modifier) fumble")
    else
        println(roll == x ? roll + modifier : roll == 1 ? roll - modifier : roll)
    end
end

```

Unfortunately, it’s still not smaller than the looped version, and it certainly makes it less readable, so not sure if there’s any real benefit to this version

---

_[View the full topic](https://discourse.julialang.org/t/opportunity-to-write-some-idiomatic-julia-code/39702)._
