1
$\begingroup$

Given a function f

f=f[x,y,z]

I would like to compute f for different random values of x,y and z greater than 0

x:=RandomReal[n]; y:=RandomReal[n]; z:=RandomReal[n]

say 20 times, and show them as a set, each value of x,y,z and f, I mean something like {x,y,z,f}. I tried

x:=RandomReal[n]; y:=RandomReal[n]; z:=RandomReal[n]
Do[Print[f[x,y,z]],20]

but it didnt work. As I said, each values of x,y and z must be different, in general.

$\endgroup$
2
  • $\begingroup$ You didn't specify a value for n $\endgroup$ Sep 25, 2015 at 20:44
  • $\begingroup$ My mistake, let us say n=10. $\endgroup$ Sep 25, 2015 at 20:52

2 Answers 2

3
$\begingroup$
ClearAll[f]
n = 5
data = RandomReal[n, {20, 3}]
Map[{Sequence @@ #, f @@ #} &, data]
$\endgroup$
1
  • $\begingroup$ It works, thank you. $\endgroup$ Sep 25, 2015 at 22:44
3
$\begingroup$

Other ways:

f[x_, y_, z_] := z + y + z
r = RandomReal[{0, 1}, {10, 3}];

r /. a : {_, _, _} :> Join[a, {f @@ a}]
r /. a : {_, _, _} :> Append[a, f @@ a]
MapThread[Append, {r, f @@@ r}]
$\endgroup$
2
  • $\begingroup$ It works nicely too. Thanks $\endgroup$ Sep 25, 2015 at 22:44
  • $\begingroup$ OK, that last one is pretty cool. $\endgroup$
    – Alan
    Sep 26, 2015 at 1:57

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.