# Generating random array in Maple

I'm trying to do simulation in Maple, but I can't figure out how to do the following:

How does one generate a set of random whole numbers in an array of 24 element (in 1 column) where the sum of the numbers has to be 10 and each numbers must be between 0 and 10?

-
What distribution governs the randomness of these numbers ? Could it be the multinomial ? If so, you should sample from multinomial distribution. – Sasha Mar 29 '12 at 12:26

1
– user2468 Mar 29 '12 at 17:38

## 2 Answers

If we produce all the partitions of 10 (of which there are 42) then we can pick `p` from that collection randomly (uniformly, if you want). There will be `np` elements in the chosen partition. We can choose `np` of the naturals 1..24, and use those as the positions of the Vector `V` to which to assign the entries of `p`.

The following acts inplace on Vector `V`. Initialization to precompute the set of all partitions of 10, and to construct V and the random generating function (1..42), takes almost no time at all.

Subsequent generation of a solution, populating V, takes about 0.00125 sec on an Intel i7.

````restart:
randomize(): # different results for each Maple session
interface(rtablesize=24):

G:=proc(v::Vector,y,all) local i,p,np,pos;
  ArrayTools:-Fill(0,v);
  p:=all[y()];
  np:=nops(p);
  pos:=[combinat[randcomb](24,np)[]];
  for i from 1 to np do v[pos[i]]:=p[i]; end do;
  NULL; # acts in-place on V
end proc:

st:=time():
All:=combinat[partition](10):
Y:=rand(1..nops(All)):
V:=Vector[row](24):
time()-st;
                           0.

G(V,Y,All);
V;

      [0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2]

G(V,Y,All);
V;

     [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 4, 0, 0, 4, 0, 0, 0, 0, 0]

G(V,Y,All);
V;

     [0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]

st:=time():
for j from 1 to 10^3 do G(V,Y,All); end do:
time()-st; # time for 1000 regenerations

                         0.125
````

The above picks from the complete set of partitions of 10 (ie. "ways to split up 10 as a sum of positive integers") with each having an equal weight, hence a uniform discrete distribution. That might not be what you want. Another way to generate each `p` is to randomly select values from {1..10} while computing a running total, stopping whenever the running total is exactly 10, and rejecting/reselecting each chosen value if it pushes the running total over 10.

-

Here is solution for $10$ elements :

````with(RandomTools):
s:=0:
A10:=Vector(1..10):
while not(s = 10) do
for n from 1 to 10 do
a := Generate(integer(range =0..10)):
A10[n]:=a;
s:=s+a:
if s >10 then
n:=0;
s:=0;
A10:=Vector(1..10);
end if;
end do;
end do;
A10;
````

For some reason I couldn't generate Vector with 24 elements .

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.7613423466682434, "perplexity_flag": "head"}