MOTIVECODEX
MOTIVECODEX

Reputation: 2752

Random number grid 4 x 6 Python 3

I am very new to Python 3 and want to generate a random grid 4x6 numbers. I've tried various ways, but end up with it printing on new lines, or printing 4x6 the same numbers on each row etc.

Some examples of code (I tried lots of various ways, but I have to retype them because I am using a virtualbox with linux to run the Python:

import random

def grid_maker(h,v):
 for y in range(1, 5):
  x = random.randint(1, 100)
  grid = [[str(x) for _ in range(v)] for _ in range (h)]
  grid[0][0] = "o"
  return grid
print ('\n'.join(''.join(row) for row in grid_maker(6,5)))

output

o85858585
8585858585
8585858585
8585858585
8585858585
8585858585

desired output

82 2 30 9 
45 65 54 14
23 4 32 42 
8 84 40 80
43 23 5 62 
23 43 3 5

Another example

for t in range(1, 5)
 i = random.randint(1, 100)
 grid = [[i], [i], [i], [i]]
 from pprint import pprint
 pprint(grid)

output

[[100], [100], [100], [100]]
[[1], [1], [1], [1]]
[[80], [80], [80], [80]]
[[12], [12], [12], [12]]

desired output

[[34], [12], [2], [15]]
[[64], [34], [53], [4]]
[[35], [34], [61], [33]]
[[34], [55], [64], [23]]

Another example

ar = []
    for t in range(1, 5):
     i = random.randint(1, 100)
     ar.append(i)
    for t in range(1, 7):
     for i in range(1, 5):
      print(ar[t-1], end='')

But this prints out 585858585555555559595959343434342222222288888888root@root:/home/user#

Thanks

Upvotes: 1

Views: 4865

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You only call random once each time in the loop so keep using the same value, call it in the list comp:

def grid_maker(h,v):
   for y in range(1, 5):
      # add call to list comp to get a new number each iteration
      grid = [[str(random.randint(1,100)) for _ in range(v)] for _ in range (h)]
   grid[0][0] = "o"
   return grid
print ('\n'.join(' '.join(row) for row in grid_maker(6,4))

o 23 47 30 
25 21 32 28
26 76 71 14
54 15 94 77
50 50 29 62
59 92 60 57

If you don't want the "o" then don't add it just remove the grid[0][0] = "o"

The first loop is also redundant you keep creating new lists but only returning one:

def grid_maker(h, v):
    return [[str(random.randint(1, 100)) for _ in range(v)] for _ in range(h)]

We can also just do it all in the fucntion:

def grid_maker(h, v):
    return '\n'.join(' '.join(row) for row in  [[str(random.randint(1, 100)) for _ in range(v)] for _ in range(h)])

print(grid_maker(6, 4)) # 6,4 not 6,5

26 96 50 46
4 57 50 80
36 24 23 66
71 33 44 15
54 15 77 54
52 99 60 99

To print lists of lists the same logic applies, you need to call randint each time:

for t in range(6):
    print([[random.randint(1,100)], [random.randint(1,100)], [random.randint(1,100)], [random.randint(1,100)]])

[[90], [57], [21], [11]]
[[84], [80], [100], [47]]
[[25], [37], [87], [97]]
[[78], [90], [100], [74]]
[[96], [83], [59], [2]]
[[83], [19], [90], [4]]

Upvotes: 1

JuniorCompressor
JuniorCompressor

Reputation: 20015

You generate only one random number in the first example:

import random
def grid_maker(h, v):
    return [
        [str(random.randint(1, 100)) for _ in range(v)]
        for _ in range(h)
    ]
print ('\n'.join(' '.join(row) for row in grid_maker(6, 5)))

For the second:

import random
ar = [
    [[random.randint(1, 100)] for t in range(1, 5)]
    for i in range(1, 5)
]
for row in ar:
    print(row)

Upvotes: 3

Related Questions