6
$\begingroup$

Here is my thing. Each element in the list has its own range.

{RandomReal[{0, 1}], RandomReal[{0, 2 Pi}]}

This code alone can only generate a point. Is it possible to generate 10 random points?

Transpose[{RandomReal[{0, 1}, 10], RandomReal[{0, 2 Pi}, 10]}]

I found this one that works at the code above. Is there anyone who can achieve that but much more elegant than me?

$\endgroup$
4
  • 2
    $\begingroup$ {#, 2 Pi #2} & @@@ RandomReal[{0, 1}, {10, 2}]? $\endgroup$
    – kglr
    Commented Jul 12, 2020 at 7:52
  • 1
    $\begingroup$ The solution by @kglr is very slick. Let me just point out that it is also the most efficient, since producing all the RandomReals in one go is highly optimized in Mathematica. $\endgroup$
    – Natas
    Commented Jul 12, 2020 at 7:56
  • 1
    $\begingroup$ and ScalingTransform[{1, 2 Pi}] @ RandomReal[{0, 1}, {10, 2}]? $\endgroup$
    – kglr
    Commented Jul 12, 2020 at 7:57
  • 2
    $\begingroup$ Ok, I was wrong. The more verbose Function[{n}, Transpose[RandomReal[{0, #}, n] & /@ {1, 2 \[Pi]}]][10] is 10 times faster than the ScalingTransform solution (and the first one @kglr posted is actually really inefficient.) $\endgroup$
    – Natas
    Commented Jul 12, 2020 at 8:09

1 Answer 1

4
$\begingroup$

Use a UniformDistribution and you can have as many ranges as you want if you supply them in a matrix as the first argument:

RandomVariate[UniformDistribution[{{0, 1}, {0, 2 Pi}}], 10]
$\endgroup$
1
  • $\begingroup$ This is also the fastest out of all the methods mentioned in the comments so far but only when dealing with more than a hundred points. Otherwise, for tens of points it has similar performance. $\endgroup$
    – flinty
    Commented Jul 12, 2020 at 12:47

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