8osm3rka
8osm3rka

Reputation: 3

How to generate all possible combinations of a 2d array in Python, if a value in this array can be between 0 and 255

So i have an empty 4*4 array, values in it can be between 0 and 255, i want to generate all possible states of this array, for example, one of the states is:

 [0, 0, 0, 0] 
 [0, 0, 0, 0] 
 [0, 0, 0, 0] 
 [0, 0, 0, 0] 

and the other is:

[245, 241, 124, 53]
[124, 11, 45, 31]
[44, 0, 124, 3]
[1, 30, 123, 31]

Is it possible to do with itertools? And if it is, how do i do it

Upvotes: 0

Views: 259

Answers (1)

Tom
Tom

Reputation: 828

The number of combinations that you are requesting here is 255^16=3.1962658*10^38 which is a very big number.

Given that 1TB = 10^12 bytes and the clock spead of CPU is 4GHz, it will take as number of molecules in a sugar cube number of storage devices and 10 billion times the age of the universe to acquire and list all of them!

In short: you can't.

You can do something like

import numpy as np
i=0
while i < 10000:
    currentstate=256*np.floor(np.rand(4,4))
/* ... do something with current state */

and randomly sample the states.

Upvotes: 1

Related Questions