-3

I want to generate number as format above and I try to use UUID() and GUID() but it is not what I want(difference format and it is hexadecimal not number)

anyone have any idea or logic to do it?

Sekny
  • 11
  • 3
  • 3
    Well, "XXXX-XXXX-XXXX" is only going to be unique **once**. When you say number, do you *really* mean number? So `1234-5678-9123` is a valid such number but `ABCD-1234-5678` is *not*? – Lasse V. Karlsen Aug 23 '17 at 06:56
  • GUID are already not 100% guaranteed to be unique, and you're removing quite a bit of cardinality, making it even worse. Might not be possible with that format, unless you store all the values that are already in use and then check against the list. – John Wu Aug 23 '17 at 06:56
  • Have you tried anything? Maybe generating 3 unique 4 digit numbers and concatenating? – Gilad Green Aug 23 '17 at 06:57
  • Is `0000-0000-0000` a valid number? – Lasse V. Karlsen Aug 23 '17 at 06:57
  • Are you familiar with [`RandomNumberGenerator`](https://msdn.microsoft.com/en-us/library/system.security.cryptography.randomnumbergenerator(v=vs.110).aspx)or [`Random`](https://msdn.microsoft.com/en-gb/en-en/library/system.random(v=vs.110).aspx)? You'll have to format it yourself. – Manfred Radlwimmer Aug 23 '17 at 06:57
  • here something familiar https://stackoverflow.com/questions/4267475/generating-8-character-only-uuids – DanilGholtsman Aug 23 '17 at 06:58
  • Since this has come up several times now: Are you looking for a **unique** or a **random** number? If **unique**: **deterministic** or **chaotic**? – Manfred Radlwimmer Aug 23 '17 at 07:04
  • 1
    @jdweng Not only it that the same basic idea as the just deleted answer by Lasse, it's also wrong. – Manfred Radlwimmer Aug 23 '17 at 07:06
  • Why are you saying it is wrong. It meets the requirements. – jdweng Aug 23 '17 at 07:53
  • Random rand = new Random(); string number = rand.Next(0, 1000000).ToString() + rand.Next(0, 1000000).ToString(); number = number.Substring(0, 3) + "-" + number.Substring(4, 4) + "-" + number.Substring(8, 4); – jdweng Aug 23 '17 at 07:54

1 Answers1

1

The simplest way to generate 1000000000000 unique numbers is just an ever-increasing sequence:

long i = 42; // whatever
string key = $"{i / 100000000:0000}-{(i / 10000) % 10000:0000}-{i % 10000:0000}";

The next time, increase i before generating the key.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • 3
    That does not guarantee uniqueness, it just generates pseudo-random numbers for each part of the string. It's unlikely to repeat keys, but it is possible. – Zohar Peled Aug 23 '17 at 07:00