##### output a list of random numbers 'x' columns wide
 output a list of random numbers 'x' columns wide adityavpratap Unladen Swallow Posts: 3 Threads: 1 Joined: Jan 2020 Reputation: Jan-13-2020, 11:07 AM (This post was last modified: Jan-13-2020, 11:30 AM by Larz60+.) 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. Reply adityavpratap Unladen Swallow Posts: 3 Threads: 1 Joined: Jan 2020 Reputation: Jan-13-2020, 01:57 PM (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")``` Reply perfringo Da Bishop Posts: 1,713 Threads: 8 Joined: Jun 2018 Reputation: Jan-13-2020, 02:52 PM (This post was last modified: Jan-13-2020, 02:53 PM by perfringo.) 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)``` I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Reply adityavpratap Unladen Swallow Posts: 3 Threads: 1 Joined: Jan 2020 Reputation: Jan-13-2020, 05:18 PM (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, Reply perfringo Da Bishop Posts: 1,713 Threads: 8 Joined: Jun 2018 Reputation: Jan-13-2020, 05:32 PM (This post was last modified: Jan-13-2020, 05:32 PM by perfringo.) (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) I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Create SQLite columns from a list or tuple? snakes 6 318 May-04-2021, 12:06 PM Last Post: snakes Unable to use random.choice(list) in async method spacedog 4 175 Apr-29-2021, 04:08 PM Last Post: spacedog adding numbers in a list Nickd12 2 321 Jan-15-2021, 12:46 PM Last Post: Serafim convert list to five columns dataframe in sequence tonycat 2 550 Sep-29-2020, 06:47 AM Last Post: tonycat Print max numbers in a list jimmoriarty 1 407 Sep-25-2020, 07:29 AM Last Post: DPaul Importing Program Wide JarredAwesome 4 529 Sep-07-2020, 04:34 PM Last Post: JarredAwesome How to append to list a function output? rama27 5 688 Aug-24-2020, 10:53 AM Last Post: DeaD_EyE importing a list of numbers into python script barrypyth 8 976 Aug-22-2020, 09:10 PM Last Post: barrypyth Read strings and numbers in columns from a file suvadip 4 833 Aug-11-2020, 09:37 PM Last Post: suvadip Summing a list of numbers Oldman45 6 694 Jul-12-2020, 05:30 PM Last Post: Oldman45

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020