Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #13931 > unrolled thread

Re: Table with condition

Started bySseziwa Mukasa <mukasa@gmail.com>
First post2013-03-26 08:05 +0000
Last post2013-03-26 08:05 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Table with condition Sseziwa Mukasa <mukasa@gmail.com> - 2013-03-26 08:05 +0000

#13931 — Re: Table with condition

FromSseziwa Mukasa <mukasa@gmail.com>
Date2013-03-26 08:05 +0000
SubjectRe: Table with condition
Message-ID<kirkrn$at4$1@smc.vnet.net>
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
>
>

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web