Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i generate a 4 x 5 matrix full of random numbers with ruby 1.8.7

Tags:

ruby

matrix

I have been trying to generate a matrix that is size 4x5 and full of random numbers by doing the following (in ruby 1.8.7):

m_rand = Matrix #create an empty matrix

n = 0
for n in 0...5        
    m=0
    for m in 0...4
        m_rand[n,m] = rand()
    end 
end

But the above does not appear to work, in particular I am not sure how to create an empty matrix of arbitrary size and then populate afterwards in ruby 1.8.7

like image 261
Zakoff Avatar asked Dec 03 '22 05:12

Zakoff


1 Answers

m_rand = Array.new(4){Array.new(5){rand} }
like image 117
steenslag Avatar answered May 20 '23 16:05

steenslag