Python Forum
output a list of random numbers 'x' columns wide
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
output a list of random numbers 'x' columns wide
#1
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
#2
(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
#3
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
#4
(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
#5
(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
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,440 Jan-05-2024, 08:30 PM
Last Post: sgrey
  random numbers, randint janeik 2 526 Nov-27-2023, 05:17 PM
Last Post: janeik
  Sample random, unique string pairs from a list without repetitions walterwhite 1 401 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 469 Oct-24-2023, 05:56 AM
Last Post: buran
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,013 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,533 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,251 Nov-13-2022, 01:27 AM
Last Post: menator01
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,429 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  sum() list from SQLAlchemy output Personne 5 4,345 May-17-2022, 12:25 AM
Last Post: Personne
  Divide a number by numbers in a list. Wallen 7 7,926 Feb-12-2022, 01:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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