Skip to content

Commit 353849e

Browse files
committed
Combinatorica Fixes...
Random[] doesn't exist so use RandomInteger or RandomReal.
1 parent 91ad2c7 commit 353849e

4 files changed

Lines changed: 41 additions & 38 deletions

File tree

mathics/packages/DiscreteMath/CombinatoricaLite.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
(***
165165
RP = Compile[{{n, _Integer}},
166166
Module[{p = Range[n],i,x,t},
167-
Do [x = Random[Integer,{1,i}];
167+
Do [x = RandomInteger[Integer,{1,i}];
168168
t = p[[i]]; p[[i]] = p[[x]]; p[[x]] = t,
169169
{i,n,2,-1}
170170
];
@@ -382,25 +382,20 @@
382382
]
383383

384384

385-
(* Not working: always returns the same sorted value.
386-
Probably Sort[] below is buggy.
387-
*)
388-
(*
389385
RandomPermutation::usage = "RandomPermutation[n] returns a random permutation of length n."
390386
RandomPermutation1[n_Integer?Positive] :=
391-
Map[ Last, Sort[ Map[({Random[],#})&,Range[n]] ] ]
387+
Map[ Last, Sort[ Map[({RandomInteger[],#})&,Range[n]] ] ]
392388

393389
RandomPermutation2[n_Integer?Positive] :=
394390
Block[{p = Range[n],i,x},
395391
Do [
396-
x = Random[Integer,{1,i}];
392+
x = RandomInteger[Integer,{1,i}];
397393
{p[[i]],p[[x]]} = {p[[x]],p[[i]]},
398394
{i,n,2,-1}
399395
];
400396
p
401397
]
402398
RandomPermutation[n_Integer?Positive] := RandomPermutation1[n]
403-
*)
404399

405400
(* Tableaux stuff not working. Hitting recursion limit....
406401
TransposeTableau::usage = "TransposeTableau[t] reflects a Young tableau t along the main diagonal, creating a different tableau."

mathics/packages/DiscreteMath/CombinatoricaV0.6.m

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -621,12 +621,12 @@
621621
NthPermutation[ RankPermutation[p]+1, Sort[p] ]
622622

623623
RandomPermutation1[n_Integer?Positive] :=
624-
Map[ Last, Sort[ Map[({Random[],#})&,Range[n]] ] ]
624+
Map[ Last, Sort[ Map[({RandomInteger[],#})&,Range[n]] ] ]
625625

626626
RandomPermutation2[n_Integer?Positive] :=
627627
Block[{p = Range[n],i,x},
628628
Do [
629-
x = Random[Integer,{1,i}];
629+
x = RandomInteger[Integer,{1,i}];
630630
{p[[i]],p[[x]]} = {p[[x]],p[[i]]},
631631
{i,n,2,-1}
632632
];
@@ -968,7 +968,7 @@
968968
Sum[ 2^(i-1) * If[ MemberQ[subset,set[[i]]], 1, 0], {i,n}]
969969
]
970970

971-
RandomSubset[set_List] := NthSubset[Random[Integer,2^(Length[set])-1],set]
971+
RandomSubset[set_List] := NthSubset[RandomInteger[Integer,2^(Length[set])-1],set]
972972

973973
GrayCode[l_List] := GrayCode[l,{{}}]
974974

@@ -1027,7 +1027,7 @@
10271027
set [[
10281028
Sort[
10291029
Table[
1030-
x=Random[Integer,{1,i}];
1030+
x=RandomInteger[Integer,{1,i}];
10311031
{s[[i]],s[[x]]} = {s[[x]],s[[i]]};
10321032
s[[i]],
10331033
{i,n,n-k+1,-1}
@@ -1126,7 +1126,7 @@
11261126
]
11271127

11281128
NextPartitionElement[n_Integer] :=
1129-
Block[{d=0,j,m,z=Random[] n PartitionsP[n],done=False,flag},
1129+
Block[{d=0,j,m,z=RandomInteger[] n PartitionsP[n],done=False,flag},
11301130
While[!done,
11311131
d++; m = n; j = 0; flag = False;
11321132
While[ !flag,
@@ -1318,9 +1318,9 @@
13181318
While [!done,
13191319
h = y[[j]] + p[[i]] - i - j;
13201320
If[ h != 0,
1321-
If[ Random[] < 0.5,
1322-
j = Random[Integer,{j,p[[i]]}],
1323-
i = Random[Integer,{i,y[[j]]}]
1321+
If[ RandomInteger[] < 0.5,
1322+
j = RandomInteger[Integer,{j,p[[i]]}],
1323+
i = RandomInteger[Integer,{i,y[[j]]}]
13241324
],
13251325
done = True
13261326
];
@@ -1333,10 +1333,10 @@
13331333
]
13341334

13351335
RandomSquare[y_List,p_List] :=
1336-
Block[{i=Random[Integer,{1,First[y]}], j=Random[Integer,{1,First[p]}]},
1336+
Block[{i=RandomInteger[Integer,{1,First[y]}], j=RandomInteger[Integer,{1,First[p]}]},
13371337
While[(i > y[[j]]) || (j > p[[i]]),
1338-
i = Random[Integer,{1,First[y]}];
1339-
j = Random[Integer,{1,First[p]}]
1338+
i = RandomInteger[Integer,{1,First[y]}];
1339+
j = RandomInteger[Integer,{1,First[p]}]
13401340
];
13411341
{i,j}
13421342
]
@@ -1758,8 +1758,8 @@
17581758
Graph[
17591759
e,
17601760
Table[
1761-
d = Random[Real,{0,fract}];
1762-
a = Random[Real,{0, 2 N[Pi]}];
1761+
d = RandomReal[Real,{0,fract}];
1762+
a = RandomReal[Real,{0, 2 N[Pi]}];
17631763
{N[v[[i,1]] + d Cos[a]], N[v[[i,2]] + d Sin[a]]},
17641764
{i,Length[e]}
17651765
]
@@ -2050,7 +2050,7 @@
20502050
]
20512051

20522052
RandomTree[n_Integer?Positive] :=
2053-
RadialEmbedding[CodeToLabeledTree[ Table[Random[Integer,{1,n}],{n-2}] ], 1]
2053+
RadialEmbedding[CodeToLabeledTree[ Table[RandomInteger[Integer,{1,n}],{n-2}] ], 1]
20542054

20552055
RandomGraph[n_Integer,p_] := RandomGraph[n,p,{1,1}]
20562056

@@ -2060,7 +2060,7 @@
20602060
Join[
20612061
Table[0,{i}],
20622062
Table[
2063-
If[Random[Real]<p, Random[Integer,range], 0],
2063+
If[RandomReal[Real]<p, RandomInteger[Integer,range], 0],
20642064
{n-i}
20652065
]
20662066
],
@@ -2082,13 +2082,13 @@
20822082
{n - Binomial[i-1,2], i}
20832083
]
20842084

2085-
RandomVertices[n_Integer] := Table[{Random[], Random[]}, {n}]
2085+
RandomVertices[n_Integer] := Table[{RandomInteger[], RandomInteger[]}, {n}]
20862086
RandomVertices[g_Graph] := Graph[ Edges[g], RandomVertices[V[g]] ]
20872087

20882088
RandomGraph[n_Integer,p_,range_List,Directed] :=
20892089
RemoveSelfLoops[
20902090
Graph[
2091-
Table[If[Random[Real]<p,Random[Integer,range],0],{n},{n}],
2091+
Table[If[RandomReal[Real]<p,RandomInteger[Integer,range],0],{n},{n}],
20922092
CircularVertices[n]
20932093
]
20942094
]

mathics/packages/DiscreteMath/CombinatoricaV0.9.m

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,12 @@
636636
NthPermutation[ RankPermutation[p]+1, Sort[p] ]
637637

638638
RandomPermutation1[n_Integer?Positive] :=
639-
Map[ Last, Sort[ Map[({Random[],#})&,Range[n]] ] ]
639+
Map[ Last, Sort[ Map[({RandomInteger[],#})&,Range[n]] ] ]
640640

641641
RandomPermutation2[n_Integer?Positive] :=
642642
Module[{p = Range[n],i,x},
643643
Do [
644-
x = Random[Integer,{1,i}];
644+
x = RandomInteger[Integer,{1,i}];
645645
{p[[i]],p[[x]]} = {p[[x]],p[[i]]},
646646
{i,n,2,-1}
647647
];
@@ -988,7 +988,7 @@
988988
Sum[ 2^(i-1) * If[ MemberQ[subset,set[[i]]], 1, 0], {i,n}]
989989
]
990990

991-
RandomSubset[set_List] := NthSubset[Random[Integer,2^(Length[set])-1],set]
991+
RandomSubset[set_List] := NthSubset[RandomInteger[Integer,2^(Length[set])-1],set]
992992

993993
GrayCode[l_List] := GrayCode[l,{{}}]
994994

@@ -1044,7 +1044,7 @@
10441044
set [[
10451045
Sort[
10461046
Table[
1047-
x=Random[Integer,{1,i}];
1047+
x=RandomInteger[Integer,{1,i}];
10481048
{s[[i]],s[[x]]} = {s[[x]],s[[i]]};
10491049
s[[i]],
10501050
{i,n,n-k+1,-1}
@@ -1143,7 +1143,7 @@
11431143
]
11441144

11451145
NextPartitionElement[n_Integer] :=
1146-
Module[{d=0,j,m,z=Random[] n PartitionsP[n],done=False,flag},
1146+
Module[{d=0,j,m,z=RandomInteger[] n PartitionsP[n],done=False,flag},
11471147
While[!done,
11481148
d++; m = n; j = 0; flag = False;
11491149
While[ !flag,
@@ -1331,9 +1331,9 @@
13311331
While [!done,
13321332
h = y[[j]] + p[[i]] - i - j;
13331333
If[ h != 0,
1334-
If[ Random[] < 0.5,
1335-
j = Random[Integer,{j,p[[i]]}],
1336-
i = Random[Integer,{i,y[[j]]}]
1334+
If[ RandomInteger[] < 0.5,
1335+
j = RandomInteger[Integer,{j,p[[i]]}],
1336+
i = RandomInteger[Integer,{i,y[[j]]}]
13371337
],
13381338
done = True
13391339
];
@@ -1346,10 +1346,10 @@
13461346
]
13471347

13481348
RandomSquare[y_List,p_List] :=
1349-
Module[{i=Random[Integer,{1,First[y]}], j=Random[Integer,{1,First[p]}]},
1349+
Module[{i=RandomInteger[Integer,{1,First[y]}], j=RandomInteger[Integer,{1,First[p]}]},
13501350
While[(i > y[[j]]) || (j > p[[i]]),
1351-
i = Random[Integer,{1,First[y]}];
1352-
j = Random[Integer,{1,First[p]}]
1351+
i = RandomInteger[Integer,{1,First[y]}];
1352+
j = RandomInteger[Integer,{1,First[p]}]
13531353
];
13541354
{i,j}
13551355
]
@@ -1773,8 +1773,8 @@
17731773
Graph[
17741774
e,
17751775
Table[
1776-
d = Random[Real,{0,fract}];
1777-
a = Random[Real,{0, 2 N[Pi]}];
1776+
d = RandomReal[Real,{0,fract}];
1777+
a = RandomReal[Real,{0, 2 N[Pi]}];
17781778
{N[v[[i,1]] + d Cos[a]], N[v[[i,2]] + d Sin[a]]},
17791779
{i,Length[e]}
17801780
]

test/test_combinatorica.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def test_combinatorica():
5656
"Permutations uses lexographic order"
5757
),
5858

59+
("RandomPermutation1[20] === RandomPermutation2[20]",
60+
"False",
61+
"Not likey two of 20! permutations will be the same (different routines)"
62+
),
63+
("RandomPermutation1[20] === RandomPermutation1[20]",
64+
"False",
65+
"Not likey two of 20! permutations will be the same (same routine)"
66+
),
5967
("RankPermutation[{8, 9, 7, 1, 6, 4, 5, 3, 2}]", "321953", "RankPermutation"),
6068
(
6169
"Permute[{5,2,4,3,1}, InversePermutation[{5,2,4,3,1}]]",

0 commit comments

Comments
 (0)