Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 0.41 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do i generate a 4 x 5 matrix full of random numbers with ruby 1.8.7
  2. m_rand = Matrix #create an empty matrix
  3.  
  4. n = 0
  5. for n in 0...5        
  6.     m=0
  7.     for m in 0...4
  8.         m_rand[n,m] = rand()
  9.     end
  10. end
  11.        
  12. m_rand = Array.new(4){Array.new(5){rand} }
  13.        
  14. require 'matrix'
  15.  
  16. rows = (0...5).map do
  17.   (0...4).map do
  18.     rand()
  19.   end
  20. end
  21.  
  22. m_rand = Matrix[rows]
  23.        
  24. Matrix.build(rows, cols) { |row, col| rand }