# Create matrix with random numbers

**URL:** <https://www.ruby-forum.com/t/create-matrix-with-random-numbers/117760>\
**Category:** Ruby\
**Created:** [October 19, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/create-matrix-with-random-numbers/117760 "2007-10-19T23:30:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [October 19, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/create-matrix-with-random-numbers/117760/1 "2007-10-19T23:30:01Z")

</div>

I would like to start evaluating Ruby’s capabilities in processing large  
linear systems and matrices and I would like to know if there is a  
simple way to ‘randomly’ populate various matrices regardless of size or  
type.

Any help, comments, anything is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [October 19, 2007, 11:49pm UTC](https://www.ruby-forum.com/t/create-matrix-with-random-numbers/117760/2 "2007-10-19T23:49:15Z")

</div>

On 10/19/07, Jason B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would like to start evaluating Ruby’s capabilities in processing large  
> linear systems and matrices and I would like to know if there is a  
> simple way to ‘randomly’ populate various matrices regardless of size or  
> type.
> 
> Any help, comments, anything is greatly appreciated!

Not sure what you want exactly, but, maybe…

require ‘matrix’  
a, b = 2, 3  
m = Matrix[\*(Array.new(a) {Array.new(b) {rand}})]

Todd

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [October 20, 2007, 3:38am UTC](https://www.ruby-forum.com/t/create-matrix-with-random-numbers/117760/3 "2007-10-20T03:38:20Z")

</div>

> Not sure what you want exactly, but, maybe…
> 
> require ‘matrix’  
> a, b = 2, 3  
> m = Matrix[\*(Array.new(a) {Array.new(b) {rand}})]
> 
> Todd

What I would ideally like is to be able to specify the size of the  
matrix and then have a random function populate the matrix with real  
integers or, eventually, complex numbers.

Thanks for the guidance…

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [October 20, 2007, 6:56am UTC](https://www.ruby-forum.com/t/create-matrix-with-random-numbers/117760/4 "2007-10-20T06:56:47Z")

</div>

On Oct 19, 2007, at 9:38 PM, Jason B. wrote:

> integers or, eventually, complex numbers.
> 
> Thanks for the guidance…

So something like:

require ‘matrix’  
def some\_random\_function

# make something up, perhaps something complex

end  
rows, columns = 2, 3  
matrix = Matrix[\*(Array.new(rows) {Array.new(columns)  
{ some\_random\_function }})]

And, yes, that just about exactly what you were already given. 😉

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
