Julia Initialized Array / Vector is not zero but random

arrays, constants, initialization, julia

Solution

@waTeim is correct that when allocating an array it is not initialized to 0 or any specific value.

The way to allocate and initialize a new array with a specific value in Julia is with `fill()` so for your `b` you would do:

b = fill(2.0, 10)

This gives you:

10-element Array{Float64,1}:
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0
 2.0

Or if you want a row vector:

b = fill(2.0, 1, 10)

Which gives you

1x10 Array{Float64,2}:
 2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0

Problem

When I define an Array in Julia: ``` z = Array(Float64, 1) ``` it appears a random value is assigned. Sometimes it is 0.0, but mostly it is something like 3.78692e-316. Is this behaviour intended? And how do I initialize a "constant" vector with 10 values, such as `b = [2.0 2 2 2]`?

Original source