Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Table with condition

37 views
Skip to first unread message

Šerých Jakub

unread,
Mar 24, 2013, 11:23:27 PM3/24/13
to
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


Bob Hanlon

unread,
Mar 26, 2013, 4:05:01 AM3/26/13
to
Module[{ri, list},
list = {ri = RandomInteger[10]};
While[ri != 5, AppendTo[list, ri = RandomInteger[10]]];
list]

{2, 2, 5}

AppendTo is fairly inefficient. If the condition is such that the list
is likely to be very long, the following may be a better approach:

Module[{ri, list},
list = {ri = RandomInteger[10]};
While[ri != 5, list = {list, ri = RandomInteger[10]}];
list // Flatten]

{2, 8, 2, 10, 6, 2, 6, 5}


Bob Hanlon

Sseziwa Mukasa

unread,
Mar 26, 2013, 4:05:11 AM3/26/13
to
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

Bill Rowe

unread,
Mar 26, 2013, 4:05:33 AM3/26/13
to
On 3/24/13 at 11:23 PM, Ser...@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?

What you might want to use is NestWhileList. Using your example
of RandomInteger you could do it as:

In[9]:= Most@
NestWhileList[(#; RandomInteger[{0, 10}]) &,
RandomInteger[{0, 10}], (# != 5) &]

Out[9]= {4,8,10,3,9,6,4,0,6,3,2,1,7,7,4,8,3,2}

What might be more efficient when each result really doesn't
depend on the previous result could be something like:

In[16]:= list = {}; r = 0;
While[r != 5, list = {list, r = RandomInteger[{0, 10}]}];
Most@Flatten[list]

Out[18]= {2,9,9,0,6,4,0,0,7,7,4,7}

Note, the issue of whether to generate a list of random integers
then use TakeWhile isn't just a question of efficiency. No
matter what length list you generate, there is always some
probability of it not having a 5. By pre-selecting the list
length you eliminate the possibility of having longer lists
which could occur.


Alexei Boulbitch

unread,
Mar 27, 2013, 3:54:31 AM3/27/13
to
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

Hi, Jakub,

This might be the way to do it. The function makeList makes the job, its parameter is your limit list number (in your case it was 5):

makeList[z_Integer] := Module[{f},
f[x_] := 1;
NestWhileList[f[#]*RandomInteger[{1, 10}] &, 1, # <= z &] // Most
];

Let us try. The following will return the nestedlist whose each next sublist has elements smaller or equal than the next number from the list Range[9]:

makeList /@ Range[9]

{{1}, {1}, {1, 1, 1}, {1, 4}, {1}, {1, 1, 5, 5}, {1, 1, 7, 3, 6, 7, 4,
7, 3, 6, 7, 2, 7, 4, 7, 6, 6, 6, 7, 2}, {1, 1, 7, 1, 6, 7, 4, 2, 1,
7, 2, 4, 2, 4, 3, 5}, {1, 1, 7, 8, 8, 8, 1, 7, 6}}

Have fun, Alexei



Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.b...@iee.lu




0 new messages