Re: while loop through a list
- To: mathgroup at smc.vnet.net
- Subject: [mg128164] Re: while loop through a list
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 20 Sep 2012 00:23:46 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
On 9/19/12 at 4:57 AM, tarpanelli at libero.it wrote:
>Hello i've this problem to solve:
>
>let us suppose I've a function f which returns a random number and
>I start with an intial list of n random number I would like to check
>if each one of the element of this list is below zero and if it is
>applies the function f till when the new random number is above 0.
>This should be repeated for each element of the list such that at
>the end all the list random numbers will be above 0.
There is no need for an explicit loop to do this. For example:
In[1]:= t = RandomReal[{-1, 1}, 5]
Out[1]= {0.0760855,-0.527881,-0.937804,-0.945365,0.866552}
In[2]:= t /. x_?Negative :> RandomReal[]
Out[2]= {0.0760855,0.953119,0.215987,0.342604,0.866552}
But do note, it is far more efficient to generate random values
in the range desired in the first place.