Re: APL, deal
- To: mathgroup <mathgroup at yoda.physics.unc.edu>
- Subject: Re: APL, deal
- From: HAY at leicester.ac.uk
- Date: Sat, 22 AUG 92 17:29:03 GMT
Below is Richard Gayloylords functional code; then a simple Do program that
simulates it and seems to be slightly faster. Then there are two analogous
versions that list the selections in the order in which they were chosen. Again
the Do one is faster.
First,Richard's code
deal1[lis_List,n_Integer]:=
Complement[
lis,
Nest[Delete[#,Random[Integer,{1,Length[#]}]]&,lis,n]
]
deal1[lis,60];//Timing
{0.8833333333333328597*Second, Null}
Using Do,
deal1Do[lis_List,n_Integer] :=
Block[{rest = lis},
Do[
rest = Delete[rest,Random[Integer,{1,Length[rest]}]],
{n}
];
Complement[lis,rest]
]
deal1Do[lis,60];//Timing
Orderless versions.
Note the technique of accumulating nested lists and flattening at the end rather
than appending elements in the course of the computation.
dealOrderlessDo[lis_List,n_Integer] :=
Block[{ans,rest,rand},
{ans, rest} = {{},lis};
Do[
rand = Random[Integer,{1,Length[rest]}];
ans = {ans,rest[[rand]]};
rest = Delete[rest,rand],
{n}
];
ans
]//Flatten
dealOrderlessDo[lis,60]//Timing
{0.9833333333333325044*Second,
{132, 14, 51, 175, 188, 76, 187, 54, 169, 199, 5, 23, 174, 124, 109,
85, 1, 105, 24, 126, 58, 66, 36, 171, 34, 57, 35, 2, 127, 128, 52,
177, 194, 6, 141, 106, 44, 20, 153, 67, 139, 170, 179, 69, 160, 95,
164, 104, 129, 197, 71, 70, 84, 137, 31, 50, 159, 78, 135, 82}}
dealOrderlessF[lis_List,n_Integer] :=
Nest[
{ {#1,#2[[#3]]},
Delete[#2,#3],
Random[Integer, {1,Length[#2] -1} ]
}&@@#&,
{{},lis,Random[Integer, {1,Length[ lis ]}]},
n
]//First//Flatten
dealOrderlessF[lis,60];//Timing
{1.066666666666668206*Second, Null}