Bottom Page

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.
Larz60+ wrote Jan-13-2020, 11:30 AM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#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")
Quote
#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.
Quote
#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,
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  What is the best way to set application-wide config values? ajorona 1 106 May-07-2020, 05:03 PM
Last Post: buran
  json.dumps list output qurr 12 546 Apr-08-2020, 10:13 PM
Last Post: micseydel
  Assign the sum of 2 consecutive numbers in a list to a varibale Fenaz 3 230 Apr-05-2020, 10:30 PM
Last Post: bowlofred
  List of n random elements medatib531 5 396 Mar-07-2020, 07:09 PM
Last Post: jefsummers
  If item in list = true, Output = xx kroh 0 159 Feb-19-2020, 09:17 AM
Last Post: kroh
  Can i prevent the random generator to generate already used numbers? MauserMan 3 269 Jan-05-2020, 04:44 PM
Last Post: MauserMan
  How do I name a list while generating numbers on the fly? Pleiades 6 368 Dec-03-2019, 05:47 PM
Last Post: jefsummers
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 5 1,171 Nov-21-2019, 09:02 PM
Last Post: Pleiades
  Finding MINIMUM number in a random list is not working Mona 5 347 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Subtract 11 from entire list of quoted numbers Pleiades 1 229 Nov-14-2019, 10:26 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)