Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive


  • To: mathgroup at
  • Subject: [mg889] Challenge!
  • From: Paul E Howland <PEHOWLAND at>
  • 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:

The function I have written to do this is:

	swap[list1_List, list2_List] :=
               M = Length[list1];
               Do [AppendTo[
                            If[Random[]>0.5, list1[[i]], list2[[i]]]
                   {i, M}

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?


  • Prev by Date: LinearSolve precision quirks (Was: Re: Lyapunov Equation)
  • Next by Date: Programming: List Structure Manipulation
  • Previous by thread: LinearSolve precision quirks (Was: Re: Lyapunov Equation)
  • Next by thread: Re: Challenge!