Quantcast
Channel: Creating a fast random array generator - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 5

Creating a fast random array generator

$
0
0

I want to create N x N array of random numbers. (N is an integer).The constraints are, each element should be between 0 to l(integer) ( here 0<= l <= N) and the sum of elements in each row should be less than equal to NAll elements are integers.

I have to call this random array generator function many times ( e.g. 10^6 ) and thus this has to be fast.

I wrote the code with procedural programming (very slow), but looking for a way to write it without loops.

For e.g. N=10, l=8, I want to generate 10x10 array with each element between 0 to 8 such that each row sums up to 10. I want to generate this array a million times.

My Code:

Nport = 10;l = 8;y = {};While[True,  x = RandomChoice[Range[0, l], {10^5, Nport}]; AppendTo[y, Select[x, Total[#] <= Nport &]];  If [Length[y] >= Nport, Break[]] ] // AbsoluteTimingTake[Flatten[y, 1], Nport]

Please Help

I am using Mathematica 9


Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>