-3

What is the best way to place numbers (1,2,3) randomly in a matrix [10, 10] in python?

I want to have an appearance of

1 = 20 times
2 = 30 times
3 = 50 times
martineau
  • 119,623
  • 25
  • 170
  • 301
SEPS
  • 95
  • 4
  • 1
    Rather than the "best" way, have you tried _any_ way? Use `np.random.choice`, which takes a probability parameter, and `reshape` the output – roganjosh Jun 09 '18 at 13:49
  • 1
    Although that won't guarantee how often a value will appear. If it needs to be guaranteed, create the array with the values, shuffle it, then reshape. – roganjosh Jun 09 '18 at 13:57

3 Answers3

1

The principle here is to create a list/array with the values appearing a set number of times, shuffling that list/array and then reshaping it.

The starting point to generate the data:

a = [1 for x in range(20)]
b = [2 for x in range(30)]
c = [3 for x in range(50)]

full_array = a + b + c

A pure python approach might use this slightly adapted:

import random

def chunks(l, n):
    n = max(1, n)
    return [l[i:i+n] for i in range(0, len(l), n)]

random.shuffle(full_array)
matrix = chunks(full_array, 10)

If you use numpy then things become easier:

import numpy as np

full_array = np.array(full_array)
np.random.shuffle(full_array)

matrix = full_array.reshape(10, 10)
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • I would appreciate if the downvoter can explain what I'm missing. The question was left open for quite a while so I assumed the consensus was that it should be answered. It's possible I made a mistake in my answer. – roganjosh Jun 09 '18 at 14:29
  • In fact, the question has no close votes, so I can only assume that my downvote is for an incorrect output, but I don't see the issue. – roganjosh Jun 09 '18 at 14:36
  • Not my DV but my guess would be because it is sort of a half-baked NumPy solution. Creating Python lists first like this is not all that NumPy-like. Again, I don't think that warrants a DV. – miradulo Jun 09 '18 at 14:37
  • @miradulo so the whole thing should be in numpy? The question isn't even tagged with that; my answer suggests that they switch. So I should probably go for a pure python approach? – roganjosh Jun 09 '18 at 14:39
  • Yeah exactly - I'd either write a Python solution or a NumPy solution (or both), preferably Python since no NumPy tag as you mentioned. This is somewhere in between. – miradulo Jun 09 '18 at 14:40
  • 1
    @miradulo fair enough, I will go for both. – roganjosh Jun 09 '18 at 14:40
  • @miradulo I made the changes but I'm not sure it makes sense to try create the starting point in numpy? Unless I'm missing something, it's easier to just start with the list comprehensions and concat them. – roganjosh Jun 09 '18 at 15:24
  • One option: start with an empty Numpy array of the designated size, and assign the values to indexed slices of it. For larger arrays this will be much much faster. Besides that this looks good :) – miradulo Jun 09 '18 at 16:12
0

I think a one-liner does exist for this. But until then this should work.

import numpy as np
temp=np.hstack((np.array([1]*20), np.array([2]*30), np.array([3]*50)))
np.random.shuffle(temp)
temp=temp.reshape(10, 10)
print(temp)
Kris
  • 518
  • 3
  • 13
-1

Maybe the best option is use numpy [1]

np.random.randint(5, size=(2, 4))

[1]https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html

  • 1
    This does not consider the frequency of values in the output at all. The question is specific on that. – roganjosh Jun 09 '18 at 14:01