To expand on what Vega mentioned, there are any number of
NuGet packages available for generating all kinds of randomness.
Here is an example I use to create a pseudo Mexican social security number for an HR system I help write automation for.
/// <summary>
/// Create a string in the format of the Mexican IMSS
/// </summary>
/// <param name="dob">New hire's date of birth.</param>
/// <returns>non-valid IMSS value</returns>
/// <remarks>This returns a string that matches the IMSS format it is NOT a simulated value</remarks>
[UserCodeMethod]
public static string GenerateMxImss(string dob)
{
Random rnd = new Random();
StringBuilder myImss = new StringBuilder();
DateTime birthdate = StringToDate(dob);
// 1-2 The number of the branch office where the Social Security Number was designated
myImss.Append(System.Convert.ToInt32(Math.Floor(rnd.Next(0, 9))).ToString());
myImss.Append(System.Convert.ToInt32(Math.Floor(rnd.Next(0, 9))).ToString());
// 3-4 The year of inscription to the Social Security
myImss.Append(birthdate.AddYears(12).ToString("yy"));
// 5-6 The year of birth of the Social Security Number owner
myImss.Append(birthdate.ToString("yy"));
// 7-10 The progressive number of procedure for the IMSS. (This digit is provided exclusively by the Institute as it regards the Folio number of such procedure).
myImss.Append(System.Convert.ToInt32(Math.Floor(rnd.Next(0, 9))).ToString());
myImss.Append(System.Convert.ToInt32(Math.Floor(rnd.Next(0, 9))).ToString());
myImss.Append(System.Convert.ToInt32(Math.Floor(rnd.Next(0, 9))).ToString());
myImss.Append(System.Convert.ToInt32(Math.Floor(rnd.Next(0, 9))).ToString());
// 11 The verification digit.
myImss.Append(System.Convert.ToInt32(Math.Floor(rnd.Next(0, 9))).ToString());
Report.Log(ReportLevel.Info, "Generated IMSS ID: " + myImss.ToString());
return myImss.ToString();
}
**Note: I wrote this in VB .Net and ran it through
Telerik's code converter It's also in need of some refactoring as you can see I keep repeating the same code over and over.
In your case you could do something like this
Dim rnd As New Random
Dim myVal As New StringBuilder
Do
myVal.Append(System.Convert.ToInt32(Math.Floor(rnd.Next(0, 9))).ToString())
Loop Until myVal.Length >= 10
Where you just change the length by adjusting the >= 10 to your need.
All this being said, it's important to keep in mind that generating a random number may not be ideal depending on your system. For example if the number is supposed to represent something (such as a Mexican Social Security Number) the DB may accept any non-duplicate value, but downstream an error could be caused by the fact that (as was my case) the randomly generated number wasn't appropriately derived from the date of birth.
It would also make sense to add a function to check the database to make sure your randomly generated number doesn't already exist. And if you're going to go that far, I'd probably run a query on that field and return the max and create my new number by adding 1 to the max value and avoid this whole mess all together.