# Creating an array/matrix with a specified range of randomly generated numbers

**URL:** <https://discourse.julialang.org/t/creating-an-array-matrix-with-a-specified-range-of-randomly-generated-numbers/15471>\
**Category:** New to Julia\
**Created:** [September 25, 2018, 2:27pm UTC](https://discourse.julialang.org/t/creating-an-array-matrix-with-a-specified-range-of-randomly-generated-numbers/15471 "2018-09-25T14:27:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [September 25, 2018, 2:27pm UTC](https://discourse.julialang.org/t/creating-an-array-matrix-with-a-specified-range-of-randomly-generated-numbers/15471/1 "2018-09-25T14:27:21Z")

</div>

Hello,

How would someone create a 5x5 array (matrix?) with randomly generated values of 0 or 1 (in Julia 1.0)?

And, if someone could clarify the difference between an array and matrix:

Array: numbers that can be grouped horizontally, vertically, or both ?  
Matrix: numbers grouped both horizontally and vertically ?

Thank you!

---

<div class="post-metadata">

**Author:** ![ric.cioffi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ric.cioffi/32/18373_2.png) [@ric.cioffi](https://discourse.julialang.org/u/ric.cioffi)\
**Post date:** [September 25, 2018, 2:50pm UTC](https://discourse.julialang.org/t/creating-an-array-matrix-with-a-specified-range-of-randomly-generated-numbers/15471/2 "2018-09-25T14:50:10Z")

</div>

EDIT ON: I’ll edit my answer for future reference as it is the most visible. As others have suggested below, the easiest way to do this is to just write

```julia
julia> rand([0, 1], 5, 5)
5×5 Array{Int64,2}:
 0 1 0 0 0
 0 1 0 0 1
 0 1 0 0 0
 0 0 0 0 0
 1 1 1 1 0

```

or, alternatively, `rand(0:1, 5, 5)`.  
EDIT OFF: I’ll leave the rest of the post as it is. It might be still instructive about the use of dots and conversions.

You can create a 5x5 array of randomly generated values _between_ 0 and 1 with:

```julia
julia> rand(5,5)
5×5 Array{Float64,2}:
 0.974959 0.898156 0.639462 0.599058 0.360013
 0.368191 0.322947 0.156321 0.762705 0.16691 
 0.977952 0.578899 0.0581204 0.470908 0.81315 
 0.505665 0.858624 0.39044 0.0380672 0.640897
 0.36901 0.508695 0.737936 0.0884765 0.311747

```

Now you can `round()` them and, to make sure the rounding is done “element-wise”, use the dot notation:

```julia
julia> round.(rand(5,5))
5×5 Array{Float64,2}:
 0.0 1.0 1.0 0.0 1.0
 1.0 1.0 0.0 0.0 0.0
 1.0 0.0 0.0 0.0 0.0
 0.0 1.0 1.0 1.0 0.0
 1.0 1.0 0.0 1.0 0.0

```

Notice that, if you need the 0 and 1 to be integers instead of floats you can either do `Int.(round.(rand(5,5)))` or `Int.(rand(Bool,5,5))` (the latter will generate a 5x5 matrix of `true`s and `falses`s which can then be converted into zeros and ones.

There is no deep difference between an array and a matrix. A matrix is just a name for two-dimensional array (in the same way a vector is just a name for a one-dimensional array). In fact:

```julia
julia> typeof(Matrix(undef,5,5))
Array{Any,2}

```

Here is a link to the docs for the dotted syntax [https://docs.julialang.org/en/stable/manual/mathematical-operations/#man-dot-operators-1](https://docs.julialang.org/en/stable/manual/mathematical-operations/#man-dot-operators-1)  
and also a blog post (if you want to know more): [https://julialang.org/blog/2017/01/moredots](https://julialang.org/blog/2017/01/moredots)  
For more on arrays you can check the documentation: [https://docs.julialang.org/en/stable/manual/arrays/](https://docs.julialang.org/en/stable/manual/arrays/)

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [September 25, 2018, 3:34pm UTC](https://discourse.julialang.org/t/creating-an-array-matrix-with-a-specified-range-of-randomly-generated-numbers/15471/3 "2018-09-25T15:34:11Z")

</div>

You can also tell `round` what type you want it to return as the first argument, so this also works:

```julia
julia> round.(Int, rand(5,5))
5×5 Array{Int64,2}:
 1 0 1 0 1
 0 0 0 0 1
 1 0 1 1 1
 0 1 1 1 1
 0 0 1 0 0

```

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [September 25, 2018, 4:40pm UTC](https://discourse.julialang.org/t/creating-an-array-matrix-with-a-specified-range-of-randomly-generated-numbers/15471/4 "2018-09-25T16:40:35Z")

</div>

Maybe is misunderstood something but why not use `rand([0, 1], 5,5)`?

---

<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:** [September 25, 2018, 5:15pm UTC](https://discourse.julialang.org/t/creating-an-array-matrix-with-a-specified-range-of-randomly-generated-numbers/15471/5 "2018-09-25T17:15:19Z")

</div>

I kinda like

```julia
rand(0:1, 5, 5)

```

---

<div class="post-metadata">

**Author:** ![ric.cioffi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ric.cioffi/32/18373_2.png) [@ric.cioffi](https://discourse.julialang.org/u/ric.cioffi)\
**Post date:** [September 25, 2018, 6:19pm UTC](https://discourse.julialang.org/t/creating-an-array-matrix-with-a-specified-range-of-randomly-generated-numbers/15471/6 "2018-09-25T18:19:10Z")

</div>

Yeah, that’s easier. I had forgotten you can also specify a range within `rand`.
