Say we have three elements, 0,1,2 and we want to generate 1000 random vectors of length 7 with these elements, i.e.
{2,1,0,0,0,0,2}
How can we do this without duplicates among these vectors?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communitySay we have three elements, 0,1,2 and we want to generate 1000 random vectors of length 7 with these elements, i.e.
{2,1,0,0,0,0,2}
How can we do this without duplicates among these vectors?
First, there are $3^7=2187$ different 7-tuples, so there is indeed a considerable probability to encounter duplicates if we generate each 7-tuple independently. So, we use the following:
vectors=IntegerDigits[#, 3, 7] & /@ RandomSample[Range[3^7] - 1, 1000]
vectors
by 3
. More general {a,b,c}
would require vectors/.{0->a,1->b,2->c}.
$\endgroup$
RandomSample[Span[0, 3^7-1], 1000]
instead.
$\endgroup$
Jan 14, 2021 at 16:32
Here are two similar methods of generating a pseudorandom list of unique vectors. The first method uses Union
. It is a little faster than the second, which uses DeleteDuplicates
. The two methods are implemented as functions f
and g
Clear[f]
f[nvect_, ndims_, list_] := Module[{a = {}, k},
If[TrueQ[nvect <= Length[list]^ndims],
While[(k = Length[a]) < nvect,
a = Union[a, RandomChoice[list, {nvect - k, ndims}]]
]];
RandomSample[a]]
Clear[g]
g[nvect_, ndims_, list_] := Module[{a = {}, k},
If[TrueQ[nvect <= Length[list]^ndims],
While[(k = Length[a]) < nvect,
a = DeleteDuplicates@Join[a, RandomChoice[list, {nvect - k, ndims}]]
]]; a]
Since Union
returns an ordered array, function f
uses RandomSample
to return a pseudorandom permutation of the ordered array. Both functions return an empty set if you ask for an impossibly large number of unique vectors.
Timing tests on f
and g
give, for 1000 unique vectors,
tf = RepeatedTiming[f[1000, 7, {0, 1, 2}], 5];
tf // First
(* 0.002 *)
tg = RepeatedTiming[g[1000, 7, {0, 1, 2}], 5];
tg // First
(* 0.003 *)
Tuples
andRandomSample
. $\endgroup$RandomSample[Tuples[{0, 1, 2}, 7], 1000]
$\endgroup$