# Random numbers and DArrays

This function doesn’t behave as expected. When I pass it a DArray, it goes to the correct part of the loop and prints “Some sort of integer… max 127” as I expect it to, but then the map alters the DArray `v` in ways I don’t expect it to. Sometimes the DArray it returns includes `0//0`.

``````function drand_rat_similar(u::DArray, max::Integer=0)
T = eltype(u[1].den)
v = similar(u)
if T == BigInt
if max <= 0
map(t -> BigInt(rand(-127:127))//BigInt(rand(1:127)), v)
else
map(t -> BigInt(rand(-max:max))//BigInt(rand(1:max)), v)
end
elseif T<:Integer
print("Some sort of integer...")
if max <= 0
println("max 127")
map(t -> rand(-127:127)//rand(1:127), v)
else
println("max \$max")
map(t -> rand(-max:max)//rand(1:max), v)
end
end
return v
end

a = drand_rat(Int64, 100)
show(a)
c = drand_rat_similar(a)
``````

I can see that `a` is assigned properly. For example,

Rational{Int64}[73//68, -11//19, 46//1, -106//35, -9//1, -7//9, 64//33, 102//77, -17//2, -46//5, 69//95, 45//116, -5//18, 25//14, 11//39, -26//63, 125//11, -25//9, 9//10, 45//23, -73//35, -43//111, -61//72, 107//16, -44//119, 7//81, -29//124, 83//27, 53//114, 58//1, 21//31, 68//79, 14//25, 4//49, 43//57, -79//55, -4//79, -16//63, 28//9, 53//26, 41//5, -27//29, -8//57, 9//52, 3//1, 115//43, 25//12, -21//10, -28//3, 61//45, 44//3, -13//22, 71//61, 87//61, -125//122, -94//81, -98//9, 17//21, -88//101, -101//124, -61//43, 24//5, 18//29, 15//77, 1//82, -45//79, -40//29, -3//1, 16//109, -33//1, -17//94, 1//1, 125//14, 29//103, 59//88, -58//53, 39//35, 71//50, -3//53, -117//82, 107//45, 27//1, -89//124, -14//81, 71//85, 93//10, -11//4, 103//126, 8//49, -127//97, -109//40, -67//64, -2//79, 101//126, 27//91, 49//93, -44//125, 3//1, 49//36, 23//39]

But `c`, for example might be

Rational{Int64}[92212848//92212912, 92212976//92213040, 92213104//92213168, 92213232//92213296, 92213360//92213424, 92213488//92213552, 92213616//92213680, 92213744//92213808, 92213872//92213936, 92214000//92214064, 92214128//92214192, 92209160//92214320, 92214384//92214448, 92214512//92214576, 92214640//92214704, 92214768//92214832, 92214896//92214960, 92215024//92215088, 92215152//92215216, 92215280//92215344, 92215408//92215472, 92215536//92215600, 92215664//92215728, 92215792//92215856, 92215920//92215984, 92216048//92216112, 92216176//92216240, 92209160//92216368, 92216432//92216496, 92216560//92216624, 92216688//92216752, 92216816//92216880, 92216944//92217008, 92217072//92217136, 92217200//92217264, 92209160//92217392, 92217456//92217520, 92217584//92217648, 92217712//92217776, 92217840//92217904, 92217968//92218032, 92218096//92218160, 92218224//92218288, 92218352//92218416, 92218480//92218544, 92218608//92218672, 92218736//92218800, 92218864//92218928, 92218992//0, 0//0, 207422016//221107248, 218803152//248036576, 100421008//100421152, 207423472//207423472, 207423472//207423472, 207424368//207423472, 207424368//207424368, 207424368//207422016, 207422016//102841344, 207487000//207487000, 207422240//221107248, 218803152//248036576, 102404192//100416976, 207424368//207423472, 207423472//207422016, 100416976//100421008, 100421152//207423472, 207423472//207423472, 207423472//207424368, 207423472//207424368, 207424368//207424368, 207422016//207422016, 223265568//100421008, 207423472//207423472, 207487000//207487000, 207487000//207487000, 207422240//223265568, 207423472//207423472, 106938000//207422016, 207422016//207422016, 104376416//207423472, 104376416//207423472, 207424368//207422016, 207424368//207422016, 207422240//207424368, 207422016//207487000, 207487000//207424368, 207424368//207423472, 207423472//207424368, 207422016//207423472, 207423472//100416496, 207422016//100416496, 100416496//207423472, 207423472//106938000, 207422016//207422016, 207422016//207422016, 207422016//207422016, 207422016//0, 0//0, 0//0]

It’s because map doesn’t modify the input vector. It returns a new one. So `v` is never being modified in the function above, and it just returns the `similar` one.