I'm new to Python. While reading, please mention any other suggestions regarding ways to improve my Python code.
Question: How do I generate a 8xN dimensional array in Python containing random numbers? The constraint is that each column of this array must contain 8 draws without replacement from the integer set [1,8]. More specifically, when N = 10, I want something like this.
[[ 6. 2. 3. 4. 7. 5. 5. 7. 8. 4.]
[ 1. 4. 5. 5. 4. 4. 8. 5. 7. 5.]
[ 7. 3. 8. 8. 3. 8. 7. 3. 6. 7.]
[ 3. 6. 7. 1. 5. 6. 2. 1. 5. 1.]
[ 8. 1. 4. 3. 8. 2. 3. 4. 3. 3.]
[ 5. 8. 1. 7. 1. 3. 6. 8. 1. 6.]
[ 4. 5. 2. 6. 2. 1. 1. 6. 4. 2.]
[ 2. 7. 6. 2. 6. 7. 4. 2. 2. 8.]]
To do this I use the following approach:
import numpy.random
import numpy
def rand_M(N):
M = numpy.zeros(shape = (8, N))
for i in range (0, N):
M[:, i] = numpy.random.choice(8, size = 8, replace = False) + 1
return M
In practice N will be ~1e7. The algorithm above is O(n) in time and it takes roughly .38 secs when N=1e3. The time therefore when N = 1e7 is ~1hr (i.e. 3800 secs). There has to be a much more efficient way.
Timing the function
from timeit import Timer
t = Timer(lambda: rand_M(1000))
print(t.timeit(5))
0.3863314103162543