7
$\begingroup$

Is it possible to define the specific range of a RandomReal array for each coordinate? For example, the code

x = 1; y = 10; L = 20;
{RandomReal[x, L], RandomReal[y, L]} // Transpose

Gives me a list of 20 pairs where the first coordinate is a random real between 0 and 1, and the second is a random real between 0 and 10.

I suspect there is a neater way of writing this. Any ideas?

$\endgroup$
  • 1
    $\begingroup$ I don't think there is. You can use RandomPoint and Rectangle but it is not really any more concise. $\endgroup$ – Szabolcs Jan 13 at 14:58
  • $\begingroup$ I see. Thanks a lot! $\endgroup$ – sam wolfe Jan 13 at 14:59
  • $\begingroup$ One can come up with variations on your version, but I don't think they're really better. E.g. Transpose[{1, 10} RandomReal[1, {2, 20}]] $\endgroup$ – Szabolcs Jan 13 at 15:01
5
$\begingroup$

Not with RandomReal that I know of, but you can define a UniformDistribution with your required limits and draw samples from it using RandomVariate:

pts = RandomVariate[UniformDistribution[{{0, 1}, {0, 10}}], 20]

Check that it works as expected:

MinMax /@ Transpose@pts

{{0.00103093, 0.996982}, {0.530043, 9.94596}}

| improve this answer | |
$\endgroup$
4
$\begingroup$

Just marginally faster

x = 1; y = 10; L = 20;
Transpose[{RandomReal[x, L], RandomReal[y, L]}]; // RepeatedTiming

{4.2*10^-6,Null}

ReIm@RandomComplex[{0, x + I y}, L]; // RepeatedTiming

{2.5*10^-6,Null}

| improve this answer | |
$\endgroup$
1
$\begingroup$

Marco's solution is the best way to do this. An alternative procedure is to use RescalingTransform[] on RandomReal[], like so:

RescalingTransform[{{0, 1}, {0, 1}}, {{0, 1}, {0, 10}}][RandomReal[1, {20, 2}]]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.