0

I have to create a random 16x16 array filled so that the sum of the entire flattened array is 500 (e.g. the sum I'd need for the array {{0,0,1,0},{1,2,1,2},{0,0,0,0},{3,0,1,0}} is 11).

I'd like also to use only integer numbers in range form 0 to 5.

Riccardo Cazzin
  • 428
  • 2
  • 12
  • 2
    by "sum among every cell" do you mean the sum of every column, row, or both? – Jason B. Feb 22 '17 at 16:57
  • probably related: 22288 – Kuba Feb 22 '17 at 17:05
  • @Artes brings up a point I hadn't considered. Aside from the ambiguity mentioned above, do you want the array filled with real numbers or integers? – Jason B. Feb 22 '17 at 17:26
  • 1
    Edit: I meant the total sum of the array (e.g. in {{0,0,1,0},{1,2,1,2},{0,1,3,0},{0,0,0,0}} the sum I'd need is 11). Sorry if I couldn't explain properly. – Riccardo Cazzin Feb 22 '17 at 18:26
  • 1
    @RiccardoCazzin It sounds like you want # (35000/Total[#, 2]) & @ RandomReal[{0, 1}, {16, 16}] – Mr.Wizard Feb 22 '17 at 18:49
  • 1
    16x16x5 = 1280. How do you expect to get 35000?? – george2079 Feb 22 '17 at 20:00
  • Touché, @george2079, touché. I messed up two different situations: (128128, 35000) and (1616, 500). I made too many mistakes: I don't know whether I should "shamequit" – Riccardo Cazzin Feb 22 '17 at 20:03
  • 1
    This had four close votes on it for "unclear what you are asking." Rather than let it be closed that way I marked it as a duplicate as I think that is probably more useful to all involved. Of course if anyone disagrees feel free to voice your opinion! – Mr.Wizard Feb 22 '17 at 20:13
  • The restriction to a small range of allowable values makes this a significantly different question. – george2079 Feb 22 '17 at 20:47

3 Answers3

1

With the help in the comments (thank you, guys: you are awesome), I achieved what I needed. Just as simple as it looks like:

Partition[
 RandomSample[
  RandomChoice[
   IntegerPartitions[
    500, {256}, {0, 1, 2, 3, 4, 5}
    ]
   ]
  ], 16]

Thank you again!!!

Riccardo Cazzin
  • 428
  • 2
  • 12
0
Table[35000 Normalize[RandomReal[{0, 1}, 16], Total], {16}]
yode
  • 26,686
  • 4
  • 62
  • 167
David G. Stork
  • 41,180
  • 3
  • 34
  • 96
0

I give a version of every cell (4*4) is $1$

l := RandomReal[1, {4, 4}] // #/Total[#, 2] &
ArrayFlatten[{{l, l}, {l, l}}] // MatrixForm

yode
  • 26,686
  • 4
  • 62
  • 167