# [✓] Generate all sequences of n integer that range from 1 to n?

GROUPS:
 Hi everyone,I am searching for a way to generate all sequences of n integers that range from 1 to n. In other words, for example with n = 2, something like {{1,1},{1,2},{2,1},{2,2}} Does anyone know an easy way to build such a function depending only on n?Thanks! Clément
1 year ago
8 Replies
 Paul Cleary 1 Vote Is this what you are looking for? n = 2; Tuples[Range[n], {2}] 
1 year ago
 Well thank you, the function Tuples was exactly what I was looking for. But I couldn't find the right tag words to find it in the documentation... Didn't think about typing "tuples" haha!Thanks a lot! Clément
1 year ago
 Hi Clément,when looking for a specific function (a typical task!) I always try to find a "similar" one, i.e. one inside the same "topical orbit": Then in the documentation under "See Also" one most likely finds the one needed. E.g. looking up for Permutations one gets the hint for Tuples. This is in general an excellent way of exploring the wealth of functions in Mathematica.Regards -- Henrik
1 year ago
 Henrik Schachner 2 Votes Standard anwer (I am sure somebody will come up with a shorter one): tuples2[n_Integer] := Tuples[Range[n], 2] @Paul: Sorry, I did not see your anwer in time ...
1 year ago
 @Henrik Schachner I doubt shorter! Faster: possibly! List~Array~{n,n} is two characters shorter, but the output is in a slightly different form.
 Sander Huisman 1 Vote Can be done in several ways: n = 5 RepeatedTiming[f1 = Tuples[Range[n], 2];, 0.5] RepeatedTiming[f2 = Join @@ Array[List, {n, n}];, 0.5] RepeatedTiming[f3 = Join @@ Table[{i, j}, {i, n}, {j, n}];, 0.5] RepeatedTiming[f4 = Join @@ Outer[List, Range[n], Range[n]];, 0.5] RepeatedTiming[f5=Flatten[{ConstantArray[Range[n],n],Transpose[ConstantArray[Range[n],n]]},{{3,2},{1}}];,0.5] f1 == f2 == f3 == f4==f5 DeveloperPackedArrayQ /@ {f1, f2, f3, f4,f5} f1 Tuples, Outer, and ConstantArray return a packed array which can be faster. Note that Tuples can only return a 'flattened' list of pairs, while the other 3 can provide multiple levels if you need that (that is why I flatten those dimensions out using Join @@).EDIT: @Paul @Henrik Did not see you answers yet, next time I should refresh...EDIT2: I added a fifth methodEDIT3: You could even do this method (definitely not recommended): n = 15 col = {}; While[Length[col] < n^2, new = RandomInteger[{1, n}, 2]; col = Union[col, {new}]] col 
 Henrik Schachner 1 Vote Dear Sander,nice! Obviously Tuples` works fastest - in accordance with the general rule to use tailored build in functions whenever possible. But I always learn something from your code - thank you!Best regards -- Henrik