Re: Challenge!
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg963] Re: [mg889] Challenge!
- From: perkins at colorado.edu (Tyler Perkins)
- Date: Thu, 4 May 1995 03:54:16 -0400
Paul E Howland <PEHOWLAND at taz.dra.hmg.gb> wrote: >I wish to generate a third list also of length M, where the i th element >of this list is either the i th element of the first list, or the i th >element of the second list. > >It should be equally probable that the new element be chosen from the >first or second list. How 'bout this: In[1]:= (* Return a choice of one of the arguments, pseudorandomly chosen. *) RandomPick[args__] := {args}[[ Random[ Integer, Length[{args}]-1 ]+1 ]] In[2]:= MapThread[ RandomPick, {{a,b,c}, {A,B,C}} ] Out[2]= {A, B, C} In[3]:= MapThread[ RandomPick, {{a,b,c}, {A,B,C}} ] Out[3]= {a, b, C} In[4]:= MapThread[ RandomPick, {{a,b,c}, {A,B,C}} ] Out[4]= {A, B, c} To do this in one line (and be more obscure!) you could make RandomPick a pure function, and use it as the first argument of MapThread. But the neat thing about RandomPick is that you can use any nonzero number of alternative arguments, not just two: In[26]:= MapThread[ RandomPick, {{1,2,3},{11,22,33},{111,222,333}} ] Out[26]= {11, 222, 333} In[27]:= MapThread[ RandomPick, {{1,2,3},{11,22,33},{111,222,333}} ] Out[27]= {1, 222, 333} In[28]:= MapThread[ RandomPick, {{1,2,3},{11,22,33},{111,222,333}} ] Out[28]= {1, 2, 33} Thanks for the "Challenge"! Tyler Perkins perkins at colorado.edu Boulder, Colorado