Challenge!
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg889] Challenge!
- From: Paul E Howland <PEHOWLAND at taz.dra.hmg.gb>
- Date: Fri, 28 Apr 1995 00:46:49 -0400
Dear Mathgroup,
I have two lists of equal length M.
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.
eg. I have a list {a,b,c,d,e,f,g}
and another {A,B,C,D,E,F,G}
valid answers would be:
{a,B,C,d,e,F,G}
{a,b,c,d,E,f,g}
{A,B,C,D,e,f,G}
etc.
The function I have written to do this is:
swap[list1_List, list2_List] :=
Module[{newlist={},M},
M = Length[list1];
Do [AppendTo[
newlist,
If[Random[]>0.5, list1[[i]], list2[[i]]]
],
{i, M}
];
newlist
]
which works fine, but it really isn't very elegant in terms of Mathematica's
functional programming style. There must be a more compact way to do this
in terms of Mapping functions, and so on.
What is the most compact function that could achieve my goal?
Paul.