Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Table with condition
Replies: 2   Last Post: Mar 26, 2013 4:05 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Sseziwa Mukasa

Posts: 108
Registered: 8/26/07
Re: Table with condition
Posted: Mar 26, 2013 4:05 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

One approach is the NestWhileList:

Most[NestWhileList[RandomInteger[{0, 10}] &, RandomInteger[{0, 10}], # != 5 &]]

Another is a simple While loop

result = {};
Block[{random},
While[(random = RandomInteger[{0, 10}]) != 5, result = {result, random}];
Flatten[result]]

Regards,
Sseziwa

On Mar 24, 2013, at 11:23 PM, =8Aer=FDch Jakub <Serych@panska.cz> wrote:

> Dear mathgroup,
> I need to start creating list and continue until some condition is not met. For example to generate list of random numbers until the value is not 5. Yes, it is theoreticaly possible to generate list with sufficient length, and cut it on the right place afterwords, but it is very inefficient way if the computation is much harder than just generation of random numbers.
>
> TakeWhile[Table[RandomInteger[{0, 10}], {15}], # != 5 &]
>
> Using the while cycle is the other way, but I can only print values using that method, but I don't know, how to generate classical list:
>
> r = 0;
> While[r != 5, r = RandomInteger[{0, 10}]; Print[r]]
>
> What is the right solution of such a simple problem?
>
> Thanks in advance for any help
>
> Jakub
>
>






Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.