# rand(1:10) vs Int(round(10\*rand())

**URL:** <https://discourse.julialang.org/t/rand-1-10-vs-int-round-10-rand/14339>\
**Category:** Performance\
**Created:** [August 30, 2018, 11:01pm UTC](https://discourse.julialang.org/t/rand-1-10-vs-int-round-10-rand/14339 "2018-08-30T23:01:31Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 31, 2018, 7:34am UTC](https://discourse.julialang.org/t/rand-1-10-vs-int-round-10-rand/14339/9 "2018-08-31T07:34:21Z")

</div>

You might also have a look at this [Julia snippets: Basics of generating random numbers in Julia](https://juliasnippets.blogspot.com/2017/11/basics-of-generating-random-numbers-in.html) (it is Julia 0.6 based but the reasons for the bias have not changed).

Having said that for very small `N` in very tight loops I use the approximate formula as the bias is small and it is significantly faster (see the corrected benchmark below - also you have to use `1+floor` not `ceil` as `ceil` can produce `0`):

```julia
julia> f1(N=5,M=10^6) = mean(iseven(1+floor(Int, N*rand())) for i in 1:M)
f1 (generic function with 3 methods)

julia> f2(N=5,M=10^6) = mean(iseven(rand(1:N)) for i in 1:M)
f2 (generic function with 3 methods)

julia> @btime f1()
  9.020 ms (0 allocations: 0 bytes)
0.400145

julia> @btime f2()
  17.681 ms (0 allocations: 0 bytes)
0.399574

```

---

_[View the full topic](https://discourse.julialang.org/t/rand-1-10-vs-int-round-10-rand/14339)._
