# Rand and rand! seem inconsistent

**URL:** https://discourse.julialang.org/t/rand-and-rand-seem-inconsistent/11460
**Category:** General Usage
**Created:** [June 6, 2018, 5:49am UTC](https://discourse.julialang.org/t/rand-and-rand-seem-inconsistent/11460 "2018-06-06T05:49:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [June 6, 2018, 5:49am UTC](https://discourse.julialang.org/t/rand-and-rand-seem-inconsistent/11460/1 "2018-06-06T05:49:40Z")

</div>

Clearly i’m missing something…

```julia
julia> rand(UInt8(0):UInt8(15))
0x02

julia> rand!(UInt8(0):UInt8(15), zeros(UInt8,5))
ERROR: indexing not defined for UnitRange{UInt8}
Stacktrace:
 [1] setindex! at ./abstractarray.jl:967 [inlined]
 [2] rand!(::MersenneTwister, ::UnitRange{UInt8}, ::Array{UInt8,1}) at ./random.jl:692
 [3] rand!(::UnitRange{UInt8}, ::Array{UInt8,1}) at ./random.jl:313

```

Naturally my goal here is to fill an existing array with random values.

---

<div class="post-metadata">

### Author: ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)
#### Post date: [June 6, 2018, 5:54am UTC](https://discourse.julialang.org/t/rand-and-rand-seem-inconsistent/11460/2 "2018-06-06T05:54:57Z")

</div>

You have the argument order reversed, try:

```julia
julia> rand!(zeros(UInt8,5), UInt8(0):UInt8(15))
5-element Array{UInt8,1}:
 0x04
 0x02
 0x05
 0x08
 0x0e

```

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [June 6, 2018, 6:03am UTC](https://discourse.julialang.org/t/rand-and-rand-seem-inconsistent/11460/3 "2018-06-06T06:03:31Z")

</div>

oh, i see. i’m having trouble with reading comprehension :

_Populate the array A with random values. If the indexable collection coll is specified, the values are picked randomly from coll. This is equivalent to copy!(A, rand(rng, coll, size(A)))_

I think it would be more clear if rand! defined [coll] to be [S] like rand does, or is there some subtle difference in how the two routine select random values ?

Thanks !

edit: furthermore i think it’s pretty consistent in julia that the first arg is the argument which is mutated in ! functions - is that fair to say ?

---

<div class="post-metadata">

### Author: ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)
#### Post date: [June 6, 2018, 6:41am UTC](https://discourse.julialang.org/t/rand-and-rand-seem-inconsistent/11460/4 "2018-06-06T06:41:38Z")

</div>

Yeah, the first argument is almost always the one being mutated.

I think `S` is allowed to be either a type or a collection, whereas `coll` is only allowed to be a collection. If you think that could be reworded more clearly, I’m sure a PR would be very welcome!

The argument order convention used by base Julia is documented here: [https://docs.julialang.org/en/latest/manual/style-guide/#Write-functions-with-argument-ordering-similar-to-Julia’s-Base-1](https://docs.julialang.org/en/latest/manual/style-guide/#Write-functions-with-argument-ordering-similar-to-Julia's-Base-1)
