Creating a 2D array with random numbers WITHOUT NUMPY (Python)

Viewed 5405

How can I create a 2D array with random numbers without using NumPy (Python)

3 Answers

You could use the random module and populate a nested list with a list comprehension

import random

low = 0
high = 10
cols = 10
rows = 5

[random.choices(range(low,high), k=cols) for _ in range(rows)]

[[5, 7, 1, 0, 6, 5, 9, 2, 5, 6],
 [9, 2, 3, 0, 6, 7, 0, 6, 6, 3],
 [2, 7, 9, 2, 4, 5, 5, 9, 9, 4],
 [2, 6, 7, 8, 5, 1, 4, 4, 4, 4],
 [9, 2, 8, 4, 5, 2, 0, 1, 2, 1]]

For a nested list of floats, you can map each range with float:

choices = list(map(float, range(low,high)))
[random.choices(choices , k=cols) for _ in range(rows)]

[[0.0, 3.0, 9.0, 1.0, 5.0, 3.0, 7.0, 4.0, 2.0, 4.0],
 [5.0, 8.0, 7.0, 7.0, 7.0, 2.0, 9.0, 8.0, 2.0, 6.0],
 [3.0, 3.0, 1.0, 9.0, 2.0, 8.0, 7.0, 2.0, 9.0, 7.0],
 [7.0, 8.0, 1.0, 2.0, 0.0, 6.0, 7.0, 6.0, 0.0, 9.0],
 [3.0, 3.0, 3.0, 1.0, 7.0, 8.0, 3.0, 9.0, 2.0, 8.0]]
[[random.random() for _ in range(3)] for _ in range(7)]

This generates a 2D array of size [7, 3] with random float in [0, 1) interval.

You use nested list comprehensions. The outer one builds a main list while the inner one builds lists that are used as elements of the main list.


Edit

You can then tweak it for your needs. For example:

import random
import pprint

NUM_ROWS=7
NUM_COLS=3
MAX_VAL=1000.50
MIN_VAL=-MAX_VAL

pprint.pprint([
  [random.uniform(MIN_VAL, MAX_VAL) for _ in NUM_COLS]
  for _ in NUM_ROWS
])

This prints a list/array/matrix of 7 lines and 3 colums with random floats in [-1000.50, 1000.50) interval:

[[561.3985362160208, -157.9871329592354, -245.7102502320838],
 [-817.8786101352823, -528.9769041860632, 102.67728824479877],
 [-886.6488625065194, 941.0504221837489, -458.58155555154565],
 [6.69525238666165, 919.5903586746183, 66.70453038938808],
 [754.3718741592056, -121.25678519054622, -577.7163532922043],
 [-352.3158889341157, 254.9985130814921, -365.0937338693691],
 [563.0633042715097, 833.2963094260072, -946.6729221921638]]

The resulting array can be indexed with array[line][column].

import pandas as pd
from random import randint

outside_size = 10 # How many nested lists to include
inside_size = 10  # How many numbers will be in an inside list
outside_list = [] # The final list
for i in range(0, outside_size, 1):
    _list = [] # Create new "inside" (nested) list
    for j in range(0, inside_size, 1): # Populate the nested list with random numbers
        _list.append(randint(0, 100))
    outside_list.append(_list) # Add the inside (nested) list to the outside (final) list

df = pd.DataFrame(outside_list) # Create the dataframe from it
print(df)

OUTPUT:

0   1    2   3   4   5   6   7   8   9
0  63  79  100  42  98  45  80  85  71  98
1  65  38   55   5  49  19  99  87  36  74
2  49  76   71  56  54  30  90  50  96  26
3  31  46   79  38  13  66  10  31   8  59
4   0  98    7  67  87   7  95  79  94  50
5  79  44   86  83   1  79  15  80  31  79
6  86   5   19  78  78  87  77   8  43  90
7  30  59    4   4  68  85  95  34  92  48
8  65  39   28  76  12  59  28  29  15  56
9  34  22   68  57  97  69  59  62  12  29

For speed and brevity of code, you can use a nested list comprehension to get the same effect ...

outside_list = [[randint(0, 100) for j in range(0, inside_size)] for i in range(0, outside_size) ]
df = pd.DataFrame(outside_list)
Related