# Simulate random data with correlation

I would like to simulate some random data e.g. between 0 and 1, but with a given correlation. In other words some data where the correlation between two datasets is e.g. a constant or within a give range of correlations for example a minimum of 0.2 and a maximum of 0.8.

Any tips how to use e.g. RandomReal or another function to create such data?

ClearAll[dist]
dist [rho_] := CopulaDistribution[{"Binormal", rho}, {UniformDistribution[{0, 1}],
UniformDistribution[{0, 1}]}];

data1 = RandomVariate[dist[-.9], 5000];
ListPlot[data1, Frame -> True, AspectRatio -> 1]


data2 = RandomVariate[dist[.6], 5000];
ListPlot[data2, Frame -> True, AspectRatio -> 1]


• +1 nice. Checks with Correlation @@ Transpose[data1] – george2079 Mar 11 '15 at 14:22
f[r_?NumberQ,n_Integer]:={First[#],#.{r,Sqrt[1-r^2]}}&/@RandomReal[NormalDistribution[0,1],{n,2}];


Produces n pairs of numbers with the correlation r.

Brute Force:

 While[! ( .9 <
Correlation[ a = RandomReal[{0, 1}, 10], b = RandomReal[{0, 1}, 10  ]] < .91 ) ]
Correlation[ a, b]


0.900731

 {a, b} // MatrixForm


I expect this breaks down in a hurry for larger sets.