-2

I am generating 2 GUIDs and taking the first 50 chars from them. What is the better way of doing this -

string tr1 = string.Join("", Guid.NewGuid().ToString("n"));
string tr2 = string.Join("", Guid.NewGuid().ToString("n"));

string full = ( tr1 + "@" + tr2).Left(50);
Doe
  • 379
  • 3
  • 14
  • 5
  • 8
    Why you need to do that, BTW? Why not just use the generated GUID? – Rahul Sep 14 '18 at 22:29
  • 2
    Do you just want 50 random hex characters? Use RNGCryptoServiceProvider.GetBytes and then convert the resulting bytes into a hex string. If that's what you want, I'll write it up as an answer. – Flydog57 Sep 14 '18 at 22:30
  • 4
    Seems odd to need something longer than a normal GUID, those things are almost certainly enough for all purposes. – DavidG Sep 14 '18 at 22:31
  • 2
    The regular GUID created by `Guid.NewGuid` is already a "Globally Unique Identifier". Adding more numbers won't make it that much more "Globally Unique" for any normal use – Pierre-Luc Pineault Sep 14 '18 at 22:32
  • 3
    What is the idea behind using `string.Join` in this scenario? I mean, it doesn't actually join anything. Does it? – Yeldar Kurmangaliyev Sep 14 '18 at 22:37
  • 1
    @Flydog57 GUID => **G**lobally **U**nique **ID**entifier - RNGCryptoServiceProvider has focus on random and not on unique – Sir Rufo Sep 14 '18 at 22:38
  • @YeldarKurmangaliyev Well, it does join the chars from the input and the result is the same as the input. It will only force some useless workload to the cpu :o) – Sir Rufo Sep 14 '18 at 22:41
  • 4
    GUIDs are globally unique because the likelihood of picking two identical 120-something bit numbers is infinitesimal. The likelihood of picking two identical 200 bit numbers is 2 to the 70-something-th more infinitessimal – Flydog57 Sep 14 '18 at 22:43

1 Answers1

0

This isn't what you asked for, but I think it's what you want:

  using System.Security.Cryptography;

  public static string Get50RandomHexNybls()
  {
      var bytes = new byte[25];
      var rand = new RNGCryptoServiceProvider();
      rand.GetBytes(bytes);
      return BitConverter.ToString(bytes).Replace("-", string.Empty);
  }

The result looks like:

B7EB8387438D55CAEC2C9D4EE73E8B99BB146EEDA4F8A9CB48

with a different result each time. If you don't like how I converted the bytes to a hex string, take a look at "How do you convert a byte array to a hexadecimal string, and vice versa"

Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • 1
    It's a 200 bit cryptographically strong random number. It's a _**Lot**_ more unique than a Guid. See: https://blogs.msdn.microsoft.com/oldnewthing/20080627-00/?p=21823 – Flydog57 Sep 14 '18 at 22:48
  • 2
    `GUIDs are globally unique because the likelihood of picking two identical 120-something bit numbers is infinitesimal` No, GUIDs are _not_ "unique" because of that. The same article you just posted tells you why. The implementation of GUIDs focuses on the uniqueness, not the randomness. – 41686d6564 stands w. Palestine Sep 14 '18 at 22:52
  • 1
    Sorry, bad link. .NET uses version 4 Guids: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29 They are 122-bit random numbers with a few more bits to establish the algorithm. A 200-bit random number is 2 to the 78th more random. They are unique – Flydog57 Sep 14 '18 at 22:58