Random generating a key sequence

Best practices, code snippets for common functionality, examples, and guidelines.
rwn60
Posts: 10
Joined: Thu May 31, 2018 6:08 pm

Random generating a key sequence

Post by rwn60 » Thu May 31, 2018 6:21 pm

Hello,
I'm new to Ranorex and C#. How would I go about generating a random "key sequence" to be applied to a GUI field and eventually saved to the database each time I "Run"? Presently, when I Run my db checks for duplicates, so on any run past 2 my GUI will display an error and the program does not run correctly after the error.

Thanks!

User avatar
Vega
Posts: 110
Joined: Mon Jul 11, 2016 2:15 pm

Re: Random generating a key sequence

Post by Vega » Thu May 31, 2018 10:06 pm

Since Ranorex is .NET based you can always refer to C# / VB.NET to do something like this. It really depends on your criteria for what you want to include in your random value... totally random alpha numeric, numeric only, append to a value etc.

Here is an example of a Random number generator which will pick a value between 5 and 10 in C#:

https://www.dotnetperls.com/random

Alternatively, you could have your test pull a value from a source that is already generating a random value like below:

http://watchout4snakes.com/wo4snakes/Random/RandomWord

So as part of your test, you could navigate to the above page and pull the Random word listed as your value. I personally wouldn't use this approach because you need to go out of your way to get the value but I just wanted to give you an idea of what was possible.

If this isn't what you were looking for, let me know more specifically what you are looking for and I'll see what I can recommend.

Vaughan.Douglas
Posts: 250
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Random generating a key sequence

Post by Vaughan.Douglas » Fri Jun 01, 2018 12:50 pm

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. :roll:
Doug Vaughan

rwn60
Posts: 10
Joined: Thu May 31, 2018 6:08 pm

Re: Random generating a key sequence

Post by rwn60 » Fri Jun 01, 2018 1:22 pm

Thanks! Vega and Doug, I'll start investigating all your suggestions...