Re: simple question if/while loop
- To: mathgroup at smc.vnet.net
- Subject: [mg40147] Re: simple question if/while loop
- From: Bill Rowe <listuser at earthlink.net>
- Date: Sat, 22 Mar 2003 05:10:24 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On 3/21/03 at 2:36 AM, atelesforos at hotmail.com (Orestis Vantzos) wrote: >Finally the functional approach: NestWhile[ 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 If[#>.5,#,0]&/@Table[Random[],{4}] also fails since it will only generate random values between 0.5 and 1. My suggestion should have been If[#>0.5,Randomp[],0]&/@Table[Random[],{4}] However, if the goal is to have an array of 4 random values between 0.5 and 1, then the better approach would be Table[Random[Real,0.5,1],{4}] 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.