MathGroup Archive 2007

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

Search the Archive

Re: neat way to program minimum of sum

Hi Przemyslaw,

Yes, the random numbers are generated in the loop.  However, you have
the list of sums at each step, so extracting the x values is

i = 1;
f = NestWhileList[
    Function[x, i++; x + Random[]],
    # < 10 &]
xlist = Flatten@{First[f], Rest[f] - Most[f]}

To better understand what has been calculated, evaluate the following
to generate a table of results:

  Transpose[{Range[i], xlist, f}],
  TableHeadings -> {None, {"i", "x[i]", "Running Total"}}

So to look at the fourth random value, use: xlist[[4]]

To answer your questions, take a look at what NestWhileList does.  The
command has the following form:
NestWhileList[ function, expression, test ]

NestWhileList applies the function to expression and saves the result.
If the test condition is still met, it will apply the function to the
previous result and store the new result.  Again, it checks the test
function to determine if it should repeat applying the function to the
latest result.  Because we're using NestWhileList, the results from
all iterations of the loop are returned in a list.

So, the #<10& is a test function that checks that the current result
is less than 10. The "#" and "&" construction is shorthand for a
pure function.  This is a function where # represents the argument to
the function.

Likewise, Function[x, i++;x+Random[]] is a function that calculates
the next term in the sum.  The function takes some argument, generates
a random number, adds the two together and returns the result.  The
variable x is just a placeholder and has no meaning other than it
represents the argument to the function.

Hope that helps.

On Wednesday, April 18, 2007 at 12:40 GMT +0200, Przemyslaw Kaminski

> Thanks for the answer. I can't test the code right now, but I don't
> quite understand that Function[x, i++; x+Random[]] part... It seems
> that the random values are generated inside the loop. What if I want
> to process them later? How do I get, say, x[4]?

> And what means '# < 10' ? Where's the parameter t

> Regards,
> P. Kaminski

> PS I made a mistake posting my question. Should be
> f[t]=min{n>0 : x[1] + .. + x[n] < t}

> 2007/4/17, Januk <ggroup at>:
>> Hi,
>> You could try:
>> i=1;
>> NestWhileList[i = 1;
>> f = NestWhileList[
>>       Function[x, i++; x + Random[]],
>>        Random[],
>>       # < 10 &];
>> Transpose[{Range[i], f}]
>> Good luck!
>> On Apr 16, 8:08 pm, cge... at (P. Kaminski) wrote:
>> > Hi
>> > I need to program the following function:
>> >
>> > f(t) = max{n>0 : X1 + ... + Xn > t}
>> >
>> > where Xi are some random numbers. I've done it using Module and Wile:
>> >
>> > x[i_]:=x[i]=Random[]
>> > f[t_]:=f[t]=Module[{i}, i=1; While[Total[Array[x, i]]<=t, i++]; Return[i-1]];
>> >
>> > This works but requires Module and three instructions within. Can it
>> > be simplified to a true one-liner, preferably with some functional
>> > programming trick?
>> >
>> > Thanks in advance,
>> > P. Kaminski

  • Prev by Date: Intersection question
  • Next by Date: Re: how to get the table 2
  • Previous by thread: Re: neat way to program minimum of sum
  • Next by thread: Re: neat way to program minimum of sum