3
$\begingroup$

I'm new to Mathematica. I need to generate a list of 1000 triplets of random numbers {r, p, t}, so that 0 < r < 1, 0 < p < 2 π and 1 < t < 10, with t being an integer.

I've found the two functions RandomReal and ``RandomInteger`, but can't seem to obtain what I need -- namely not a list.

I need to pass this list of triplets to a function f[r_, p_, t_].

$\endgroup$
  • 4
    $\begingroup$ list=Transpose@{RandomReal[{0,1},1000],RandomReal[{0,2 Pi},1000], RandomInteger[{1,10},1000]} is the most straight forward. Then you can do f@@@list $\endgroup$ – N.J.Evans Jul 22 '16 at 20:27
  • 3
    $\begingroup$ From what distributions are you sampling? Do you just want uniform samples chosen? In that case, use @N.J.Evans's solution. Otherwise, please be more specific. $\endgroup$ – march Jul 22 '16 at 20:43
  • 2
    $\begingroup$ Regarding N.J.Evans's comment, @@@ is shorthand for Apply with a levelspec of {1}; see the documentation for Apply for more information. $\endgroup$ – Mr.Wizard Jul 22 '16 at 21:44
5
$\begingroup$

For the sake of demonstration, let us suppose

f[r_Real, p_Real, t_Integer] := r Sin[p t]

With[{n = 10}, 
  triples = 
    Transpose[
      {RandomReal[{0, 1}, n], 
       RandomReal[{0, 2 Pi}, n], 
       RandomInteger[{1, 10}, n]}]];

Note that this last definition can generate a list of triples of any size by changing the value given to n.

Now, N. J. Evans' answer is the most straight forward.

f @@@ triples

{0.273082, 0.603514, -0.296061, 0.09708, 0.174089, -0.255095, 0.173383, 0.304217, 0.170979, 0.160158}

However, because Mathematica can apply it powerful pattern matcher to arguments, you can also do this

ff[{r_, p_, t_}] := f[r, p, t]
ff /@ triples

or even this.

fff[triples : {{_, _, _} ..}] := f @@@ triples
fff[triples]
$\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.