Algorithm Questions

• To: mathgroup at smc.vnet.net
• Subject: [mg29386] Algorithm Questions
• From: "Hu Zhe" <huzhe at public3.sta.net.cn>
• Date: Sat, 16 Jun 2001 02:47:57 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Hi,
I am wondering that if Mathematica has lazy-evaluation features. Say I
am going to extract 100 lists from the permutation lists of {1,
2,...,100} (Permutation[Range[100]]) randomly.

If I program like this:

largeList = Permutation[Range[100]];

indx = Table[Random[Integer, {1, 100!}], {100}];

Part[largeList, indx]

the program is very slow, since it works out all the 100! elements for
the largeList, while I only need 100 items from it.

Can I program like this ? Say,

take 100 (somePermutationFunction 100)

so that the permutation stops after 100 lists were taken. (I learned
that it's called lazy-evaluation from Haskell.)

The second question is that:

I want the 100 lists randomly taken to be all different.

So I program this way:

While[Length[a] != 100,
a = (Table[Random[Integer, {1, n!}], {100}] // Union)];

there must be more efficient solutions, especially to combine the
solution with the first question?

So my slow program is like this, deeply welcome suggestions to improve
it:

randomList[n_, k_] :=
Module[{a},
While[Length[a] != k,
a = (Table[Random[Integer, {1, n!}], {k}] // Union)];
Part[Permutations[Range[n]], a]]

Sincerely,
Hu Zhe

```

• Prev by Date: Re: Can Mathematica evaluate step by step
• Next by Date: Re: A New User
• Previous by thread: Re: Can Mathematica evaluate step by step
• Next by thread: Re: Algorithm Questions