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

Answer by Kellen Myers for Creating a fast random array generator

$
0
0

I'm going to just scrap my previous answer and hopefully this will actually work to include your parameter l, which I'm writing L to avoid confusion (whereas your N is n to avoid syntax errors).

MyNorm[v_] := n v/Total[v]MakeRow[] := Block[{v}, v = MyNorm[RandomReal[1, {n}]]; While[Max[v] > L, v = MyNorm[RandomReal[1, {n}]]]; v];MakeArray[] := Table[MakeRow[], {n}]n = 50; L = 5;Timing[For[i = 1, i <= 100, i++, MakeArray[]]][[1]]/100(* Output: 0.00062400 *)

So for $n=50$ and $l=5$, we can get these cooked up relatively quickly, so that $10^6$ of them will take 10 minutes. Notice that $l$ being close to 1 is a serious problem, so making $l$ smaller will definitely make this worse (in terms of speed):

n = 50; L = 1.6;Timing[For[i = 1, i <= 100, i++, MakeArray[]]][[1]]/100(* Output: 0.21372137 *)

We can test this out to make sure it works:

Total /@ MakeArray[](* Output: {50., 50., .... , 50.}Max /@ MakeArray[](* Output: {1.58351, 1.54753, 1.57146, .... , 1.59688}

However, if $l\sim N$, there shouldn't be much of a slowdown. For example:

n = 1000; L = 100;Timing[For[i = 1, i <= 100, i++, MakeArray[]]][[1]]/100(* Output: 0.02948419 *)

According to my tests, this is as fast as David's answer, but of course includes the parameter L. Generally if L is not too small, this should be as fast as David's, I think.

This means that, on my PC anyway, you'll get 10^6 of these (when $n=1000$ and $L$ is not too small) over the course of a couple days. Not great, but not infeasible if this is a serious project that's going to be using that large a timescale. On the other hand, $1000\times 1000$ is quite big.. no idea if you want them that big.

Any input for us on exactly how big $n$ is going to be? What is the timeframe you are thinking about? What else is taking up computing time in your algorithm? If it's really $n=10$ and $L=8$, then you can generate $10^6$ of these in a minute or two.

EDIT FOR INTEGER VALUES

If you want integers in these arrays, the question is substantially different. I don't think you're going to get much faster than this:

Needs["Combinatorica`"];n = 32; L = 20;MakeRow[] := Block[{v = RandomComposition[n, n]},  While[Max[v] > L,   v = RandomComposition[n, n]   ];  v  ]MakeArray[] := Table[MakeRow[], {n}]10^3 Timing[For[i = 1, i <= 10^3, i++, MakeArray[]]][[1]]

The output there indicates that generating $10^6$ of these will take about 45 minutes. I don't think you can beat RandomComposition, and I doubt even more strongly that you could bring this down to the efficiency of the non-integer version. Maybe this 45 minutes isn't best, but it's not going to be remotely close to 5 minutes (or 1 minute) without a very clever implementation.


Viewing all articles
Browse latest Browse all 5

Trending Articles



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