Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A C# helper class for generating random USA Social Security numbers. This was used in automated unit tests.
using System;
namespace Helpers
{
/// <summary>
/// Social Security Number Generator.
/// </summary>
public static class SocialSecurityNumberGenerator
{
/// <summary>
/// Generates a random USA Social Security Number.
/// </summary>
/// <param name="delimiter">Optional delimiter.</param>
/// <returns>Social Security Number.</returns>
public static string GenerateSocialSecurityNumber(string delimiter = "")
{
var random = new Random(Guid.NewGuid().GetHashCode());
var threeDigitSegment = random.Next(132, 921);
var twoDigitSegment = random.Next(12, 83);
var fourDigitSegment = random.Next(1423, 9211);
return $"{threeDigitSegment}{delimiter}{twoDigitSegment}{delimiter}{fourDigitSegment}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment