1414- function zeroVector(dimension)
1515- function unitBasisVector(dimension,pos)
1616- function axpy(scalar,vector1,vector2)
17+ - function randomVector(N,a,b)
1718- class Matrix
18- - squareZeroMatrix(N)
19+ - function squareZeroMatrix(N)
20+ - function randomMatrix(W,H,a,b)
1921"""
2022
2123
2224import math
25+ import random
2326
2427
2528class Vector (object ):
@@ -196,6 +199,20 @@ def axpy(scalar,x,y):
196199 return (x * scalar + y )
197200
198201
202+ def randomVector (N ,a ,b ):
203+ """
204+ input: size (N) of the vector.
205+ random range (a,b)
206+ output: returns a random vector of size N, with
207+ random integer components between 'a' and 'b'.
208+ """
209+ ans = zeroVector (N )
210+ random .seed (None )
211+ for i in range (N ):
212+ ans .changeComponent (i ,random .randint (a ,b ))
213+ return ans
214+
215+
199216class Matrix (object ):
200217 """
201218 class: Matrix
@@ -328,5 +345,20 @@ def squareZeroMatrix(N):
328345 row .append (0 )
329346 ans .append (row )
330347 return Matrix (ans ,N ,N )
348+
349+
350+ def randomMatrix (W ,H ,a ,b ):
351+ """
352+ returns a random matrix WxH with integer components
353+ between 'a' and 'b'
354+ """
355+ matrix = []
356+ random .seed (None )
357+ for i in range (H ):
358+ row = []
359+ for j in range (W ):
360+ row .append (random .randint (a ,b ))
361+ matrix .append (row )
362+ return Matrix (matrix ,W ,H )
331363
332364
0 commit comments