I want to define a function based on a random choice from a list of possible functions.
For example, suppose the possible functions are {Sqrt,Log}
. Then I want to define f[x]
to be a (randomly chosen) one of f[x_]=Sqrt[x]
and f[x_]:=Log[x]
What is a good way to do this? (and have it work with user defined functions in the list, like {Sqrt,Log,x^2+x}
My feeling is that I should make a function that takes a list of functions as an input, and randomly chooses one of them. I have an example of this below, but I have three concerns/issues with the below code:
- using
?fun1
givesfun1=Log
, notfun1=Log[x]
- I don't know how(/whether) this matters, but something nags at me that it does
- If I want a function that is either
f[x_]:=Sqrt[x]
f[x_]:=Log[x+1]
, I don't know how to incorporate this "+1" part - I should probably be using
:=
not=
in the example code below
I imagine 1 and 3 can likely be corrected by some small tweak to the code, whereas for 2 all I can think of is using Which
, but this seem unwieldy for long lists of possible functions.
I don't know how to go about correcting this hence my question.
Example code:
SeedRandom[10];
chooseFun[listFun_] :=
Module[{randVal}, randVal = RandomInteger[{1, Length[listFun]}];
listFun[[randVal]]
];
fun1 = chooseFun[{Sqrt, Log}];
fun2 = chooseFun[{Sqrt, Log}];
Edit: Note: Its not a requirement that the function take a list of functions as an input. I would be okay, say, defining ListOfFunctions
before calling SeedRandom in the below code.
Also, an additional concern/question: Normally when we do something like f[x_]:=x^2
it assigns the RHS to the LHS. But in the case here I dont want to assign the LHS to the RHS, but the something based on the output of the RHS. (Sorry if I have not explained this well)
Edit 2: Heres another attempt I have made. This one uses a function that defines another function (based on a name that is the input).
- Is this a good approach?
- Note here
?test
givestest[x$_] := listFun[x$][[randNum$61000]]
.
Clear["Global`*"]
SeedRandom[10];
listFun[x_] := {Sqrt[x], Log[x + 1]};
chooseRandFunc[name_] := Module[{randNum},
randNum = RandomInteger[{1, Length[listFun[x]]}];
name[x_] := listFun[x][[randNum]]
];
chooseRandFunc[test]
Edit 3: It seems the question was not clear, so I went back to try to clarify. Sorry to the existing answers
ClearAll[chooseFun]; chooseFun=RandomChoice
? $\endgroup$chooseFun[x]
evaluate properly though, in particular if one of the options is a user defined function likeLog[x+1]
orx^2_x
as opposed to a system one likeLog
orSqrt
? (I ask because I usually think aboutf[x_]:=x^2
as saying "take x^2 and replace x by whatever is given", butchooseFun=RandomChoice
doesn't take an input which confuses me.) $\endgroup$chooseFunb[{Sqrt, Log}]@xx
or asfa =chooseFunb[{Sqrt, Log}]; fa@x
$\endgroup$choseFun
asClearAll[chooseFun]; chooseFun := RandomChoice[{Sqrt, Log}];
ThenchooseFun[t]
givesSqrt[t]
orLog[t]
at each invocation. $\endgroup$chooseFunb
in the earlier comment (i.e. why is there a "b" there?). Second, withClearAll[chooseFun] chooseFun := RandomChoice[{Sqrt, Log}];
,chooseFun[t]
would (potentially) switch fromSqrt
toLog
on different evaluations, wouldn't it? $\endgroup$