MathGroup Archive 2003

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

Search the Archive

Re: simple question if/while loop

  • To: mathgroup at
  • Subject: [mg40147] Re: simple question if/while loop
  • From: Bill Rowe <listuser at>
  • Date: Sat, 22 Mar 2003 05:10:24 -0500 (EST)
  • Sender: owner-wri-mathgroup at

On 3/21/03 at 2:36 AM, atelesforos at (Orestis Vantzos) wrote:

>Finally the functional approach:

  Module[{a}, If[(a = Random[]) > .5, Join[#, {a}], #]] &, 
  Length[#] < 4 &

>will return what you want.

Although this may be a correct interpretation of the *intent* of the the original poster, it does not do the same thing. The orignial poster initialized each element of the array to zero and changed elements to a random value whenever another random value was greater than 0.5.

In fact, I just realized my suggestion of


also fails since it will only generate random values between 0.5 and 1. My suggestion should have been


However, if the goal is to have an array of 4 random values between 0.5 and 1, then the better approach would be


No need for an If statement or While loop. Addtionally, Random is not called more times than the number of random values desired.

Trying to help someone correct their code when the intent is not clearly stated certainly makes an interesting problem. Without a clear statement of what the code is intended to do, it is difficult to determine what errors have been made or more efficient approaches.

  • Prev by Date: Running 4.0.1 under MacOSX-Classic?
  • Next by Date: Finite Fields and Polynomials
  • Previous by thread: Re: simple question if/while loop
  • Next by thread: Mathematica Developer Conference in April 2003