MathGroup Archive 2012

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

Search the Archive

Re: while loop through a list


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.




  • Prev by Date: Copying Text From CDF Output Cells
  • Next by Date: Solved: Weighting in simultaneous NonlinearModelFits
  • Previous by thread: while loop through a list
  • Next by thread: Re: while loop through a list