Random Six Character Strings Without Collisions











up vote
2
down vote

favorite












I want to generate a random list of six characters that only contain characters from "0...9" and "A...Z".



For this I can define



ToCharacterCode[{"ABCDEFGHIJKLMONPQRSTUVWXYZ", "0123456789"}]


I can modify the answer here as



rndnew = FromCharacterCode@
RandomChoice[Join @@ Range[{48, 65}, {57, 90}], #] &


I can now generate five of these strings by doing



rndnew[{5, 6}]


This results in something like



{"35UVUS", "F7WIJG", "PQSBHF", "PIHTSW", "R3MDM6"}


My question is how can I guarantee that these strings have no collisions (I know this is a very large space)? Is there a better way to code this using random over the range of the size of this set (like a linear congruential generator with that range) to make sure the strings are unique or is there a facility in Mathematica to do that?










share|improve this question


















  • 5




    replace RandomChoice with RandomSample?
    – kglr
    3 hours ago










  • @kglr: I will certainly look into that. Thanks for the pointer!
    – Moo
    3 hours ago






  • 2




    When a function is close to what you want, look at the See Also section of that function's documentation.
    – Bob Hanlon
    3 hours ago















up vote
2
down vote

favorite












I want to generate a random list of six characters that only contain characters from "0...9" and "A...Z".



For this I can define



ToCharacterCode[{"ABCDEFGHIJKLMONPQRSTUVWXYZ", "0123456789"}]


I can modify the answer here as



rndnew = FromCharacterCode@
RandomChoice[Join @@ Range[{48, 65}, {57, 90}], #] &


I can now generate five of these strings by doing



rndnew[{5, 6}]


This results in something like



{"35UVUS", "F7WIJG", "PQSBHF", "PIHTSW", "R3MDM6"}


My question is how can I guarantee that these strings have no collisions (I know this is a very large space)? Is there a better way to code this using random over the range of the size of this set (like a linear congruential generator with that range) to make sure the strings are unique or is there a facility in Mathematica to do that?










share|improve this question


















  • 5




    replace RandomChoice with RandomSample?
    – kglr
    3 hours ago










  • @kglr: I will certainly look into that. Thanks for the pointer!
    – Moo
    3 hours ago






  • 2




    When a function is close to what you want, look at the See Also section of that function's documentation.
    – Bob Hanlon
    3 hours ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I want to generate a random list of six characters that only contain characters from "0...9" and "A...Z".



For this I can define



ToCharacterCode[{"ABCDEFGHIJKLMONPQRSTUVWXYZ", "0123456789"}]


I can modify the answer here as



rndnew = FromCharacterCode@
RandomChoice[Join @@ Range[{48, 65}, {57, 90}], #] &


I can now generate five of these strings by doing



rndnew[{5, 6}]


This results in something like



{"35UVUS", "F7WIJG", "PQSBHF", "PIHTSW", "R3MDM6"}


My question is how can I guarantee that these strings have no collisions (I know this is a very large space)? Is there a better way to code this using random over the range of the size of this set (like a linear congruential generator with that range) to make sure the strings are unique or is there a facility in Mathematica to do that?










share|improve this question













I want to generate a random list of six characters that only contain characters from "0...9" and "A...Z".



For this I can define



ToCharacterCode[{"ABCDEFGHIJKLMONPQRSTUVWXYZ", "0123456789"}]


I can modify the answer here as



rndnew = FromCharacterCode@
RandomChoice[Join @@ Range[{48, 65}, {57, 90}], #] &


I can now generate five of these strings by doing



rndnew[{5, 6}]


This results in something like



{"35UVUS", "F7WIJG", "PQSBHF", "PIHTSW", "R3MDM6"}


My question is how can I guarantee that these strings have no collisions (I know this is a very large space)? Is there a better way to code this using random over the range of the size of this set (like a linear congruential generator with that range) to make sure the strings are unique or is there a facility in Mathematica to do that?







list-manipulation random






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 3 hours ago









Moo

6921515




6921515








  • 5




    replace RandomChoice with RandomSample?
    – kglr
    3 hours ago










  • @kglr: I will certainly look into that. Thanks for the pointer!
    – Moo
    3 hours ago






  • 2




    When a function is close to what you want, look at the See Also section of that function's documentation.
    – Bob Hanlon
    3 hours ago














  • 5




    replace RandomChoice with RandomSample?
    – kglr
    3 hours ago










  • @kglr: I will certainly look into that. Thanks for the pointer!
    – Moo
    3 hours ago






  • 2




    When a function is close to what you want, look at the See Also section of that function's documentation.
    – Bob Hanlon
    3 hours ago








5




5




replace RandomChoice with RandomSample?
– kglr
3 hours ago




replace RandomChoice with RandomSample?
– kglr
3 hours ago












@kglr: I will certainly look into that. Thanks for the pointer!
– Moo
3 hours ago




@kglr: I will certainly look into that. Thanks for the pointer!
– Moo
3 hours ago




2




2




When a function is close to what you want, look at the See Also section of that function's documentation.
– Bob Hanlon
3 hours ago




When a function is close to what you want, look at the See Also section of that function's documentation.
– Bob Hanlon
3 hours ago










4 Answers
4






active

oldest

votes

















up vote
3
down vote













Just changing RandomChoice to RandomSample doesn't help, since RandomSample just means that none of the characters are repeated, and clearly you want to allow repeated characters, since one of your strings is "35UVUS". One idea is to oversample and remove duplicates. For example:



SeedRandom[1]
Take[
DeleteDuplicates[rndnew[{10, 6}]],
UpTo[5]
]



{"K1263G", "SXVXHC", "UO2PBL", "EC1FTJ", "0TKLEH"}




Another possibility is to index the possible random strings, and then do a random sample from the possible indices. For your case, the number of possible strings is simply 36^6. To convert from an index to a string:



characters = Join[CharacterRange["A","Z"], CharacterRange["0","9"]];

fromIndex[index_, len_] := StringJoin[
characters[[1+IntegerDigits[index, 36, len]]]
]


Then, a function that returns n samples of length len strings:



sample[n_, len_] := fromIndex[#, len]& /@ RandomSample[1 ;; 36^len, n]


Example:



SeedRandom[1]
sample[5, 6]



{"0621O7", "0WH6XW", "ODLV4Z", "KWSN6U", "AMOSFA"}







share|improve this answer























  • Is there a way to generate some number of these, keep the state of the RNG and use it to reseed from that point moving forward so we can maintain uniqueness of all six-characters over time?
    – Moo
    1 hour ago










  • @Moo Not that I'm aware of. Do you know how many unique strings you want in total?
    – Carl Woll
    1 hour ago










  • If I am doing my math correctly, we have 6-character strings where strings can repeat, then there are $26$ letters plus $10$ digits in the pool of characters, giving us $36$ in all per character. This means we would have $36^6 = 2176782336$ combinations.
    – Moo
    1 hour ago






  • 1




    @Moo There are 36^6 total strings, but how many do you want to generate? There is no point in using a random approach if you want to generate all of them.
    – Carl Woll
    1 hour ago










  • CarlWoll: The issue is that I would generate something like $10000$ at a time, use them and then generate a new batch and over time, duplicates matter. Maybe there is a better way based on that, but I haven't thought about it. I am not sure I would ever use up the total number shown, but it can certainly be over several million of them over time.
    – Moo
    47 mins ago




















up vote
2
down vote













RandomSample[
Union[CharacterRange["a", "z"], CharacterRange["0", "9"]], 6]


or



RandomSample[Flatten[CharacterRange @@@ {{"a", "z"}, {"0", "9"}}], 6]


or



RandomSample[Join @@ (CharacterRange @@@ {{"a", "z"}, {"0", "9"}}), 6]


This guarantees no collisions because RandomSample selects elements without replacement.






share|improve this answer























  • How does does guarantee no collisions?
    – Moo
    2 hours ago


















up vote
2
down vote













Inspired by some of the other answers and comments, here's a rather silly brute-force approach. We can RandomSample a range that we can use to enumerate all combinations.



range = Range[0, 36^6 - 1];
(* This takes a little while: 15 seconds on my laptop. *)

chars = Union[CharacterRange["a", "z"], CharacterRange["0", "9"]];

makeString[n_] := ToString@Part[chars, 1 + IntegerDigits[n, 36, 6]];


Then



makeString /@ RandomSample[range, 1000]


will always return a bunch of unique strings.






share|improve this answer




























    up vote
    2
    down vote













    Following is what you want. Unfortunately it takes time to generate all possible set for n=6. But for n=5 it works. And it is guaranteed that there is no repeated element.



    val = Tuples[Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 5];

    RandomSample[val, 5]


    Much smart way is



    DeleteDuplicates[
    Table[RandomSample[
    Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], 5]]


    It is very unlikely to generate the same element.



    n = 10000;
    Length@DeleteDuplicates[
    Table[RandomSample[
    Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]] ==
    n



    True




    But when you increase n



    Table[n = 100000;
    n - Length@
    DeleteDuplicates[
    Table[RandomSample[
    Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]],
    10] // Max



    5




    This means 5 elements are repeated when we generate 1000000 sample.. Of course, it might be larger than 5.






    share|improve this answer





















      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "387"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f187829%2frandom-six-character-strings-without-collisions%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      Just changing RandomChoice to RandomSample doesn't help, since RandomSample just means that none of the characters are repeated, and clearly you want to allow repeated characters, since one of your strings is "35UVUS". One idea is to oversample and remove duplicates. For example:



      SeedRandom[1]
      Take[
      DeleteDuplicates[rndnew[{10, 6}]],
      UpTo[5]
      ]



      {"K1263G", "SXVXHC", "UO2PBL", "EC1FTJ", "0TKLEH"}




      Another possibility is to index the possible random strings, and then do a random sample from the possible indices. For your case, the number of possible strings is simply 36^6. To convert from an index to a string:



      characters = Join[CharacterRange["A","Z"], CharacterRange["0","9"]];

      fromIndex[index_, len_] := StringJoin[
      characters[[1+IntegerDigits[index, 36, len]]]
      ]


      Then, a function that returns n samples of length len strings:



      sample[n_, len_] := fromIndex[#, len]& /@ RandomSample[1 ;; 36^len, n]


      Example:



      SeedRandom[1]
      sample[5, 6]



      {"0621O7", "0WH6XW", "ODLV4Z", "KWSN6U", "AMOSFA"}







      share|improve this answer























      • Is there a way to generate some number of these, keep the state of the RNG and use it to reseed from that point moving forward so we can maintain uniqueness of all six-characters over time?
        – Moo
        1 hour ago










      • @Moo Not that I'm aware of. Do you know how many unique strings you want in total?
        – Carl Woll
        1 hour ago










      • If I am doing my math correctly, we have 6-character strings where strings can repeat, then there are $26$ letters plus $10$ digits in the pool of characters, giving us $36$ in all per character. This means we would have $36^6 = 2176782336$ combinations.
        – Moo
        1 hour ago






      • 1




        @Moo There are 36^6 total strings, but how many do you want to generate? There is no point in using a random approach if you want to generate all of them.
        – Carl Woll
        1 hour ago










      • CarlWoll: The issue is that I would generate something like $10000$ at a time, use them and then generate a new batch and over time, duplicates matter. Maybe there is a better way based on that, but I haven't thought about it. I am not sure I would ever use up the total number shown, but it can certainly be over several million of them over time.
        – Moo
        47 mins ago

















      up vote
      3
      down vote













      Just changing RandomChoice to RandomSample doesn't help, since RandomSample just means that none of the characters are repeated, and clearly you want to allow repeated characters, since one of your strings is "35UVUS". One idea is to oversample and remove duplicates. For example:



      SeedRandom[1]
      Take[
      DeleteDuplicates[rndnew[{10, 6}]],
      UpTo[5]
      ]



      {"K1263G", "SXVXHC", "UO2PBL", "EC1FTJ", "0TKLEH"}




      Another possibility is to index the possible random strings, and then do a random sample from the possible indices. For your case, the number of possible strings is simply 36^6. To convert from an index to a string:



      characters = Join[CharacterRange["A","Z"], CharacterRange["0","9"]];

      fromIndex[index_, len_] := StringJoin[
      characters[[1+IntegerDigits[index, 36, len]]]
      ]


      Then, a function that returns n samples of length len strings:



      sample[n_, len_] := fromIndex[#, len]& /@ RandomSample[1 ;; 36^len, n]


      Example:



      SeedRandom[1]
      sample[5, 6]



      {"0621O7", "0WH6XW", "ODLV4Z", "KWSN6U", "AMOSFA"}







      share|improve this answer























      • Is there a way to generate some number of these, keep the state of the RNG and use it to reseed from that point moving forward so we can maintain uniqueness of all six-characters over time?
        – Moo
        1 hour ago










      • @Moo Not that I'm aware of. Do you know how many unique strings you want in total?
        – Carl Woll
        1 hour ago










      • If I am doing my math correctly, we have 6-character strings where strings can repeat, then there are $26$ letters plus $10$ digits in the pool of characters, giving us $36$ in all per character. This means we would have $36^6 = 2176782336$ combinations.
        – Moo
        1 hour ago






      • 1




        @Moo There are 36^6 total strings, but how many do you want to generate? There is no point in using a random approach if you want to generate all of them.
        – Carl Woll
        1 hour ago










      • CarlWoll: The issue is that I would generate something like $10000$ at a time, use them and then generate a new batch and over time, duplicates matter. Maybe there is a better way based on that, but I haven't thought about it. I am not sure I would ever use up the total number shown, but it can certainly be over several million of them over time.
        – Moo
        47 mins ago















      up vote
      3
      down vote










      up vote
      3
      down vote









      Just changing RandomChoice to RandomSample doesn't help, since RandomSample just means that none of the characters are repeated, and clearly you want to allow repeated characters, since one of your strings is "35UVUS". One idea is to oversample and remove duplicates. For example:



      SeedRandom[1]
      Take[
      DeleteDuplicates[rndnew[{10, 6}]],
      UpTo[5]
      ]



      {"K1263G", "SXVXHC", "UO2PBL", "EC1FTJ", "0TKLEH"}




      Another possibility is to index the possible random strings, and then do a random sample from the possible indices. For your case, the number of possible strings is simply 36^6. To convert from an index to a string:



      characters = Join[CharacterRange["A","Z"], CharacterRange["0","9"]];

      fromIndex[index_, len_] := StringJoin[
      characters[[1+IntegerDigits[index, 36, len]]]
      ]


      Then, a function that returns n samples of length len strings:



      sample[n_, len_] := fromIndex[#, len]& /@ RandomSample[1 ;; 36^len, n]


      Example:



      SeedRandom[1]
      sample[5, 6]



      {"0621O7", "0WH6XW", "ODLV4Z", "KWSN6U", "AMOSFA"}







      share|improve this answer














      Just changing RandomChoice to RandomSample doesn't help, since RandomSample just means that none of the characters are repeated, and clearly you want to allow repeated characters, since one of your strings is "35UVUS". One idea is to oversample and remove duplicates. For example:



      SeedRandom[1]
      Take[
      DeleteDuplicates[rndnew[{10, 6}]],
      UpTo[5]
      ]



      {"K1263G", "SXVXHC", "UO2PBL", "EC1FTJ", "0TKLEH"}




      Another possibility is to index the possible random strings, and then do a random sample from the possible indices. For your case, the number of possible strings is simply 36^6. To convert from an index to a string:



      characters = Join[CharacterRange["A","Z"], CharacterRange["0","9"]];

      fromIndex[index_, len_] := StringJoin[
      characters[[1+IntegerDigits[index, 36, len]]]
      ]


      Then, a function that returns n samples of length len strings:



      sample[n_, len_] := fromIndex[#, len]& /@ RandomSample[1 ;; 36^len, n]


      Example:



      SeedRandom[1]
      sample[5, 6]



      {"0621O7", "0WH6XW", "ODLV4Z", "KWSN6U", "AMOSFA"}








      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 1 hour ago

























      answered 1 hour ago









      Carl Woll

      66.5k385174




      66.5k385174












      • Is there a way to generate some number of these, keep the state of the RNG and use it to reseed from that point moving forward so we can maintain uniqueness of all six-characters over time?
        – Moo
        1 hour ago










      • @Moo Not that I'm aware of. Do you know how many unique strings you want in total?
        – Carl Woll
        1 hour ago










      • If I am doing my math correctly, we have 6-character strings where strings can repeat, then there are $26$ letters plus $10$ digits in the pool of characters, giving us $36$ in all per character. This means we would have $36^6 = 2176782336$ combinations.
        – Moo
        1 hour ago






      • 1




        @Moo There are 36^6 total strings, but how many do you want to generate? There is no point in using a random approach if you want to generate all of them.
        – Carl Woll
        1 hour ago










      • CarlWoll: The issue is that I would generate something like $10000$ at a time, use them and then generate a new batch and over time, duplicates matter. Maybe there is a better way based on that, but I haven't thought about it. I am not sure I would ever use up the total number shown, but it can certainly be over several million of them over time.
        – Moo
        47 mins ago




















      • Is there a way to generate some number of these, keep the state of the RNG and use it to reseed from that point moving forward so we can maintain uniqueness of all six-characters over time?
        – Moo
        1 hour ago










      • @Moo Not that I'm aware of. Do you know how many unique strings you want in total?
        – Carl Woll
        1 hour ago










      • If I am doing my math correctly, we have 6-character strings where strings can repeat, then there are $26$ letters plus $10$ digits in the pool of characters, giving us $36$ in all per character. This means we would have $36^6 = 2176782336$ combinations.
        – Moo
        1 hour ago






      • 1




        @Moo There are 36^6 total strings, but how many do you want to generate? There is no point in using a random approach if you want to generate all of them.
        – Carl Woll
        1 hour ago










      • CarlWoll: The issue is that I would generate something like $10000$ at a time, use them and then generate a new batch and over time, duplicates matter. Maybe there is a better way based on that, but I haven't thought about it. I am not sure I would ever use up the total number shown, but it can certainly be over several million of them over time.
        – Moo
        47 mins ago


















      Is there a way to generate some number of these, keep the state of the RNG and use it to reseed from that point moving forward so we can maintain uniqueness of all six-characters over time?
      – Moo
      1 hour ago




      Is there a way to generate some number of these, keep the state of the RNG and use it to reseed from that point moving forward so we can maintain uniqueness of all six-characters over time?
      – Moo
      1 hour ago












      @Moo Not that I'm aware of. Do you know how many unique strings you want in total?
      – Carl Woll
      1 hour ago




      @Moo Not that I'm aware of. Do you know how many unique strings you want in total?
      – Carl Woll
      1 hour ago












      If I am doing my math correctly, we have 6-character strings where strings can repeat, then there are $26$ letters plus $10$ digits in the pool of characters, giving us $36$ in all per character. This means we would have $36^6 = 2176782336$ combinations.
      – Moo
      1 hour ago




      If I am doing my math correctly, we have 6-character strings where strings can repeat, then there are $26$ letters plus $10$ digits in the pool of characters, giving us $36$ in all per character. This means we would have $36^6 = 2176782336$ combinations.
      – Moo
      1 hour ago




      1




      1




      @Moo There are 36^6 total strings, but how many do you want to generate? There is no point in using a random approach if you want to generate all of them.
      – Carl Woll
      1 hour ago




      @Moo There are 36^6 total strings, but how many do you want to generate? There is no point in using a random approach if you want to generate all of them.
      – Carl Woll
      1 hour ago












      CarlWoll: The issue is that I would generate something like $10000$ at a time, use them and then generate a new batch and over time, duplicates matter. Maybe there is a better way based on that, but I haven't thought about it. I am not sure I would ever use up the total number shown, but it can certainly be over several million of them over time.
      – Moo
      47 mins ago






      CarlWoll: The issue is that I would generate something like $10000$ at a time, use them and then generate a new batch and over time, duplicates matter. Maybe there is a better way based on that, but I haven't thought about it. I am not sure I would ever use up the total number shown, but it can certainly be over several million of them over time.
      – Moo
      47 mins ago












      up vote
      2
      down vote













      RandomSample[
      Union[CharacterRange["a", "z"], CharacterRange["0", "9"]], 6]


      or



      RandomSample[Flatten[CharacterRange @@@ {{"a", "z"}, {"0", "9"}}], 6]


      or



      RandomSample[Join @@ (CharacterRange @@@ {{"a", "z"}, {"0", "9"}}), 6]


      This guarantees no collisions because RandomSample selects elements without replacement.






      share|improve this answer























      • How does does guarantee no collisions?
        – Moo
        2 hours ago















      up vote
      2
      down vote













      RandomSample[
      Union[CharacterRange["a", "z"], CharacterRange["0", "9"]], 6]


      or



      RandomSample[Flatten[CharacterRange @@@ {{"a", "z"}, {"0", "9"}}], 6]


      or



      RandomSample[Join @@ (CharacterRange @@@ {{"a", "z"}, {"0", "9"}}), 6]


      This guarantees no collisions because RandomSample selects elements without replacement.






      share|improve this answer























      • How does does guarantee no collisions?
        – Moo
        2 hours ago













      up vote
      2
      down vote










      up vote
      2
      down vote









      RandomSample[
      Union[CharacterRange["a", "z"], CharacterRange["0", "9"]], 6]


      or



      RandomSample[Flatten[CharacterRange @@@ {{"a", "z"}, {"0", "9"}}], 6]


      or



      RandomSample[Join @@ (CharacterRange @@@ {{"a", "z"}, {"0", "9"}}), 6]


      This guarantees no collisions because RandomSample selects elements without replacement.






      share|improve this answer














      RandomSample[
      Union[CharacterRange["a", "z"], CharacterRange["0", "9"]], 6]


      or



      RandomSample[Flatten[CharacterRange @@@ {{"a", "z"}, {"0", "9"}}], 6]


      or



      RandomSample[Join @@ (CharacterRange @@@ {{"a", "z"}, {"0", "9"}}), 6]


      This guarantees no collisions because RandomSample selects elements without replacement.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 2 hours ago

























      answered 2 hours ago









      David G. Stork

      22.9k22051




      22.9k22051












      • How does does guarantee no collisions?
        – Moo
        2 hours ago


















      • How does does guarantee no collisions?
        – Moo
        2 hours ago
















      How does does guarantee no collisions?
      – Moo
      2 hours ago




      How does does guarantee no collisions?
      – Moo
      2 hours ago










      up vote
      2
      down vote













      Inspired by some of the other answers and comments, here's a rather silly brute-force approach. We can RandomSample a range that we can use to enumerate all combinations.



      range = Range[0, 36^6 - 1];
      (* This takes a little while: 15 seconds on my laptop. *)

      chars = Union[CharacterRange["a", "z"], CharacterRange["0", "9"]];

      makeString[n_] := ToString@Part[chars, 1 + IntegerDigits[n, 36, 6]];


      Then



      makeString /@ RandomSample[range, 1000]


      will always return a bunch of unique strings.






      share|improve this answer

























        up vote
        2
        down vote













        Inspired by some of the other answers and comments, here's a rather silly brute-force approach. We can RandomSample a range that we can use to enumerate all combinations.



        range = Range[0, 36^6 - 1];
        (* This takes a little while: 15 seconds on my laptop. *)

        chars = Union[CharacterRange["a", "z"], CharacterRange["0", "9"]];

        makeString[n_] := ToString@Part[chars, 1 + IntegerDigits[n, 36, 6]];


        Then



        makeString /@ RandomSample[range, 1000]


        will always return a bunch of unique strings.






        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          Inspired by some of the other answers and comments, here's a rather silly brute-force approach. We can RandomSample a range that we can use to enumerate all combinations.



          range = Range[0, 36^6 - 1];
          (* This takes a little while: 15 seconds on my laptop. *)

          chars = Union[CharacterRange["a", "z"], CharacterRange["0", "9"]];

          makeString[n_] := ToString@Part[chars, 1 + IntegerDigits[n, 36, 6]];


          Then



          makeString /@ RandomSample[range, 1000]


          will always return a bunch of unique strings.






          share|improve this answer












          Inspired by some of the other answers and comments, here's a rather silly brute-force approach. We can RandomSample a range that we can use to enumerate all combinations.



          range = Range[0, 36^6 - 1];
          (* This takes a little while: 15 seconds on my laptop. *)

          chars = Union[CharacterRange["a", "z"], CharacterRange["0", "9"]];

          makeString[n_] := ToString@Part[chars, 1 + IntegerDigits[n, 36, 6]];


          Then



          makeString /@ RandomSample[range, 1000]


          will always return a bunch of unique strings.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 52 mins ago









          Pillsy

          12.7k13179




          12.7k13179






















              up vote
              2
              down vote













              Following is what you want. Unfortunately it takes time to generate all possible set for n=6. But for n=5 it works. And it is guaranteed that there is no repeated element.



              val = Tuples[Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 5];

              RandomSample[val, 5]


              Much smart way is



              DeleteDuplicates[
              Table[RandomSample[
              Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], 5]]


              It is very unlikely to generate the same element.



              n = 10000;
              Length@DeleteDuplicates[
              Table[RandomSample[
              Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]] ==
              n



              True




              But when you increase n



              Table[n = 100000;
              n - Length@
              DeleteDuplicates[
              Table[RandomSample[
              Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]],
              10] // Max



              5




              This means 5 elements are repeated when we generate 1000000 sample.. Of course, it might be larger than 5.






              share|improve this answer

























                up vote
                2
                down vote













                Following is what you want. Unfortunately it takes time to generate all possible set for n=6. But for n=5 it works. And it is guaranteed that there is no repeated element.



                val = Tuples[Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 5];

                RandomSample[val, 5]


                Much smart way is



                DeleteDuplicates[
                Table[RandomSample[
                Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], 5]]


                It is very unlikely to generate the same element.



                n = 10000;
                Length@DeleteDuplicates[
                Table[RandomSample[
                Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]] ==
                n



                True




                But when you increase n



                Table[n = 100000;
                n - Length@
                DeleteDuplicates[
                Table[RandomSample[
                Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]],
                10] // Max



                5




                This means 5 elements are repeated when we generate 1000000 sample.. Of course, it might be larger than 5.






                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Following is what you want. Unfortunately it takes time to generate all possible set for n=6. But for n=5 it works. And it is guaranteed that there is no repeated element.



                  val = Tuples[Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 5];

                  RandomSample[val, 5]


                  Much smart way is



                  DeleteDuplicates[
                  Table[RandomSample[
                  Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], 5]]


                  It is very unlikely to generate the same element.



                  n = 10000;
                  Length@DeleteDuplicates[
                  Table[RandomSample[
                  Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]] ==
                  n



                  True




                  But when you increase n



                  Table[n = 100000;
                  n - Length@
                  DeleteDuplicates[
                  Table[RandomSample[
                  Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]],
                  10] // Max



                  5




                  This means 5 elements are repeated when we generate 1000000 sample.. Of course, it might be larger than 5.






                  share|improve this answer












                  Following is what you want. Unfortunately it takes time to generate all possible set for n=6. But for n=5 it works. And it is guaranteed that there is no repeated element.



                  val = Tuples[Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 5];

                  RandomSample[val, 5]


                  Much smart way is



                  DeleteDuplicates[
                  Table[RandomSample[
                  Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], 5]]


                  It is very unlikely to generate the same element.



                  n = 10000;
                  Length@DeleteDuplicates[
                  Table[RandomSample[
                  Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]] ==
                  n



                  True




                  But when you increase n



                  Table[n = 100000;
                  n - Length@
                  DeleteDuplicates[
                  Table[RandomSample[
                  Join @@ (CharacterRange @@@ {{"A", "Z"}, {"0", "9"}}), 6], n]],
                  10] // Max



                  5




                  This means 5 elements are repeated when we generate 1000000 sample.. Of course, it might be larger than 5.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 47 mins ago









                  Okkes Dulgerci

                  3,7331716




                  3,7331716






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Mathematica Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f187829%2frandom-six-character-strings-without-collisions%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                      Mangá

                      Eduardo VII do Reino Unido