How to create a m x n random matric in Python without using built in functions (numpy etc)

python @ Freshers.in

There are some scenario , where  you need to create an m x n random matric A without using built in functions. In some exams they can ask “Write a code to generate a random matrix A of size m ×n with m > n without using built in functions numpy and the entries of A must be of the form r.ddddd “

Code 

import random
m_num_rows = 4  #number of rows
n_num_cols = 3  #number of columns
random_val_start = 3.1
random_val_end = 5.8
matric_list_A = []
for row in range(m_num_rows): #row loop
    row_lst_elements = []
    for col in range(n_num_cols): # column loop
        #random.uniform : Draw samples from a uniform distribution
        row_lst_elements.append(round(random.uniform(random_val_start, random_val_end),5))
    matric_list_A.append(row_lst_elements)
for matric_row in matric_list_A:
    print (matric_row)

 

Result

"""
[3.60758, 4.4829, 5.21699]
[4.95751, 5.3181, 4.61465]
[3.35091, 5.64929, 5.12011]
[5.79038, 4.20262, 4.13534]
"""

Reference

  1. Python articles
  2. Spark Examples
  3. PySpark Blogs
  4. Bigdata Blogs
Author: user

Leave a Reply