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?
If we produce all the partitions of 10 (of which there are 42) then we can pick
pfrom that collection randomly (uniformly, if you want). There will benpelements in the chosen partition. We can choosenpof the naturals 1..24, and use those as the positions of the VectorVto which to assign the entries ofp.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.
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
pis 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.