# Draw two distinct random integers from \`1:n\`

**URL:** <https://discourse.julialang.org/t/draw-two-distinct-random-integers-from-1-n/72289>\
**Category:** Performance\
**Tags:** statistics, performance, random\
**Created:** [November 30, 2021, 11:46am UTC](https://discourse.julialang.org/t/draw-two-distinct-random-integers-from-1-n/72289 "2021-11-30T11:46:37Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 30, 2021, 2:46pm UTC](https://discourse.julialang.org/t/draw-two-distinct-random-integers-from-1-n/72289/21 "2021-11-30T14:46:27Z")

</div>

Note that this solution is broken for `n>2^32`

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 30, 2021, 2:56pm UTC](https://discourse.julialang.org/t/draw-two-distinct-random-integers-from-1-n/72289/22 "2021-11-30T14:56:12Z")

</div>

> [@lmiq](#):
>
> And that is faster than calling `rand()` twice:

No, not if you call `rand` properly, explicitly providing the rng. As mentioned.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [November 30, 2021, 3:07pm UTC](https://discourse.julialang.org/t/draw-two-distinct-random-integers-from-1-n/72289/23 "2021-11-30T15:07:41Z")

</div>

For me it is not even faster with default global rng, running 1.7 though so maybe that is the difference from Xoshiro?

```julia
julia> function iter(n,draw)
           for i in 1:1000
               draw(n)
           end
           nothing
       end
iter (generic function with 1 method)

julia> function draw1(n)
       a = rand(1:n)
       b = rand(1:n-1)
       b += (a <= b)
       return a,b
       end
draw1 (generic function with 1 method)

julia> @btime iter(3,$draw1)
  8.139 μs (0 allocations: 0 bytes)

julia> function draw5(n)
           ind = rand(0:(n * (n - 1) - 1))
           a, b = divrem(ind, n)
           a += (a >= b)
           return a + 1, b + 1
       end
draw5 (generic function with 1 method)

julia> @btime iter(3,$draw5)
  10.833 μs (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 30, 2021, 3:32pm UTC](https://discourse.julialang.org/t/draw-two-distinct-random-integers-from-1-n/72289/24 "2021-11-30T15:32:04Z")

</div>

Yes, here (1.6) with MersenneTwister the difference is of about 5% in favor of the two-rand calls. Passing the `rng` is sometimes cumbersome, so good there is a reasonable alternative 🙂 .

---

<div class="post-metadata">

**Author:** ![ultrapoci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ultrapoci/32/25356_2.png) [@ultrapoci](https://discourse.julialang.org/u/ultrapoci)\
**Post date:** [December 3, 2021, 2:09pm UTC](https://discourse.julialang.org/t/draw-two-distinct-random-integers-from-1-n/72289/25 "2021-12-03T14:09:47Z")

</div>

Probably not faster than the other solutions, but here is my take:

```julia
function draw(n)
    a = rand(0:n-1)
    b = mod(rand(0:n-2) + a + 1, n)
    a+1, b+1
end

```

```julia
julia> histogram(reinterpret(Int,[draw(3) for i = 1:100000]), nbins=3)
              ┌ ┐
   [1.0, 2.0) ┤████████████████████████████████▊ 66585
   [2.0, 3.0) ┤████████████████████████████████▊ 66562
   [3.0, 4.0) ┤█████████████████████████████████ 66853
              └ ┘
                               Frequency

```

[Previous page](https://discourse.julialang.org/t/draw-two-distinct-random-integers-from-1-n/72289.md?page=1)
