How to generate a random 8 character string with a certain format

Hi! So I’m trying to create a process where I’ll create and send a verification code, with a certain format. I’m thinking of doing it like this CCCNNNNN where C is Char and N is a number/int. So for example it will be like, HJX71395. I hope you can help me, thank you.

1 Like

Hello @Archie

You can random string and rNdom number activity for creating this. Create both and concantenate it.

WE can use random Numbers and also map Numbers to Letters for the Generation

Have a Look on
New Random.Next()
The Mod function
Chr() function

Hi,

How about the following?

System.Text.RegularExpressions.Regex.Replace(format,"C|N",Function(m) if (m.Value="C",Chr(rnd.Next(65,90)).ToString ,if(m.Value="N",Chr(rnd.Next(48,57)),"-")))

Sequence5.xaml (5.8 KB)

Regards
,

1 Like

Hey @Archie

Please try the below,

String.Join(String.Empty, Enumerable.Range(0, 3).Select(Function(i) Convert.ToChar(new Random().Next(65,90)))) + new Random().Next(11111,99999).ToString

Here is the project ref from UiPath Studio Web -
nmnithinkrishna_RandomVerificationCodeGenerator.zip (1.9 KB)

Hope this helps.

Thanks
#nK

I think this works too, though it prints the other way around. Instead of CCCNNNN, it goes NNNNNCCC.

Thanks! This works perfectly well for me.

Oh sorry for that :smiley:

Just reverse the concatenation operands :stuck_out_tongue:

Updated the above

Thanks

1 Like

Thanks! also works well using this

String.Join(String.Empty, Enumerable.Range(0, 3).Select(Function(i) Convert.ToChar(new Random().Next(65,90))))+new Random().Next(11111,99999).ToString

I think this one looks cleaner as it doesn’t need to use more than one Assign Activity.

1 Like

Cool with whichever you are comfortable :slight_smile:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.