Skip to content

Instantly share code, notes, and snippets.

@andresmoschini
Last active March 14, 2018 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andresmoschini/22407550e15fb5366709c7a042b94e6f to your computer and use it in GitHub Desktop.
Save andresmoschini/22407550e15fb5366709c7a042b94e6f to your computer and use it in GitHub Desktop.
/*
EXAMPLE EXECUTION
testname: GetNeverPonyNumber
uniques: 50000000
collisions: 0
examples: 8702a199572565e1, 84a4a866d8222e3f, 7de9de6bc852e14d
elapsed: 00:03:47.0953452
testname: GetRealyNumber
uniques: 50000000
collisions: 0
examples: xfw2av1e1ao38, opxgipe2cjkwx, 3qcn8np84hgli
elapsed: 00:02:10.8699939
testname: GetCriptoNumber
uniques: 50000000
collisions: 0
examples: 5qfvcfsa0bppo, zskhfefz1vppa, 600rajsztqdnj
elapsed: 00:02:21.0801211
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
class Program
{
const int batchSize = 50_000_000;
static readonly Random _random = new Random();
static readonly RNGCryptoServiceProvider _cryptoServiceProvider = new RNGCryptoServiceProvider();
static void Main(string[] args)
{
Test("GetNeverPonyNumber", GetNeverPonyNumber); // generated lenght 16 characters, 16 possible values => combinations: 18446744073709551616
Test("GetRealyNumber", () => GetRelayNumber(13, "1234567890abcdefghijklmnopqrstuvwxyz")); // generated lenght 13 characters, 36 possible values => combinations: 170581728179578208256 (aprox 10x > 18446744073709551616)
Test("GetCriptoNumber", () => GetCriptoNumber(13, "1234567890abcdefghijklmnopqrstuvwxyz")); // generated lenght 13 characters, 36 possible values => combinations: 170581728179578208256 (aprox 10x > 18446744073709551616)
Console.WriteLine("Enter to continue . . .");
Console.ReadLine();
}
private static void Test(string testname, Func<string> algorithm)
{
var stopWatch = Stopwatch.StartNew();
var uniques = new HashSet<string>();
var collisions = 0;
for (var i = 0; i < batchSize; i++)
{
var generated = algorithm();
if (!uniques.Add(generated))
{
collisions++;
}
}
var elapsed = stopWatch.Elapsed;
Console.WriteLine($@"
testname: {testname}
uniques: {uniques.Count()}
collisions: {collisions}
examples: {string.Join(", ", uniques.Take(3))}
elapsed: {elapsed}
");
}
static private string GetNeverPonyNumber()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray())
{
i *= ((int)b + 1);
}
return string.Format("{0:x}", i - DateTime.Now.Ticks);
}
static string GetRelayNumber(int length, string charset) =>
string.Join(string.Empty,
Enumerable.Repeat(true, length).Select(
x => charset[_random.Next(charset.Length)]));
static string GetCriptoNumber(int length, string charset)
{
var data = new byte[length];
_cryptoServiceProvider.GetNonZeroBytes(data);
return string.Join(string.Empty,
data.Select(x => charset[x % charset.Length]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment