Python Forum
output a list of random numbers 'x' columns wide - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: output a list of random numbers 'x' columns wide (/thread-23702.html)



output a list of random numbers 'x' columns wide - adityavpratap - Jan-13-2020

Hi,
I am new to programming and learning python.
I want to generate a list of random numbers and print them in a table of a specified number (say 10) columns. For example say fifty random numbers in a table of 10 columns and 5 rows
This is what I tried -

import random
ran_dom = 0
def gen_ran():
    ran_dom = round(random.random(), 5)
    print(ran_dom, end="\t")

resp = int(input("How many randoms you want to generate :"))

for i in range(resp):
    x = 0
    for x in range(10):
        gen_ran()
    print("\r")
If I want 50 randoms, the output is a table of 10 columns and 50 rows.

Awaiting suggestions from you people.
Regards,
Aditya Pratap V.


RE: output a list of random numbers 'x' columns wide - adityavpratap - Jan-13-2020

(Jan-13-2020, 11:07 AM)adityavpratap Wrote: Hi, I am new to programming and learning python. I want to generate a list of random numbers and print them in a table of a specified number (say 10) columns. For example say fifty random numbers in a table of 10 columns and 5 rows This is what I tried -
 import random ran_dom = 0 def gen_ran(): ran_dom = round(random.random(), 5) print(ran_dom, end="\t") resp = int(input("How many randoms you want to generate :")) for i in range(resp): x = 0 for x in range(10): gen_ran() print("\r") 
If I want 50 randoms, the output is a table of 10 columns and 50 rows. Awaiting suggestions from you people. Regards, Aditya Pratap V.

Solved!

The following code works to satisfaction -
import random
ran_dom = 0
def gen_ran():
    ran_dom = round(random.random(), 5)
    print(ran_dom, end="\t")

resp = int(input("How many randoms you want to generate :"))

for i in range(resp):
    if i % 10 == 0:
        print("\r")
    gen_ran()
print("\n")



RE: output a list of random numbers 'x' columns wide - perfringo - Jan-13-2020

If you need to do any actual work with data then printing is not that useful.

More common would be to built data structure and print it out if needed:

>>> import random
>>> table = [[random.random() for j in range(10)] for i in range(50)]    # will create 2D matrix 50 rows with 10 elements
>>> for row in table: 
...     print(*row, sep=' ')                                             # will print row by row, row values separated by space


If work is getting more serious then numpy has built-in method:

>>> import numpy as np
>>> np.random.rand(50,10)



RE: output a list of random numbers 'x' columns wide - adityavpratap - Jan-13-2020

(Jan-13-2020, 02:52 PM)perfringo Wrote: If you need to do any actual work with data then printing is not that useful.

More common would be to built data structure and print it out if needed:

>>> import random
>>> table = [[random.random() for j in range(10)] for i in range(50)]    # will create 2D matrix 50 rows with 10 elements
>>> for row in table: 
...     print(*row, sep=' ')                                             # will print row by row, row values separated by space


If work is getting more serious then numpy has built-in method:

>>> import numpy as np
>>> np.random.rand(50,10)
Thanks for helping. Seems to be a nice way to create a 2D table. Where can I read more about it?
Regards,


RE: output a list of random numbers 'x' columns wide - perfringo - Jan-13-2020

(Jan-13-2020, 05:18 PM)adityavpratap Wrote: Seems to be a nice way to create a 2D table. Where can I read more about it?

Documentation on python.org: list comprhensions (note Nested List Comprehensions)