0

This is a number # This is a number or letter?

Separate the random string like ??#?#-???##-#?#???-#???#-##

I need some code that generates the string as shown above. It doesn't have to be complicated.

Expected result example: 2F421-QD421-2W3FY0-3F4L1-37

I've tried using PHP and this example but wasn't able to achieve what I was looking for Generating a random numbers and letters

I am looking for a vb.net project to handle the generation so i can submit the serial into a database manually.

3
  • Please post your code?
    – J_D
    Jun 3, 2019 at 8:05
  • It's quite typical with codes like 2F421-QD421-2W3FY0-3F4L1-37 that there is a checksum involved to validate the number. Did you need anything like that? Jun 3, 2019 at 9:19
  • Basically I am needing to generate a large list of whats above so I can input it into a database, because I dont know how to do that its easier to just do it manually. Jun 4, 2019 at 10:39

3 Answers 3

1

I quite like this approach:

Dim characters = "0123456789ABCDEFGHIJKLOMNOPQRSTUVWXYZ"
Dim template = "??#?#-???##-#?#???-#???#-##"

Dim rnd = New Random()

Dim query =
    From t In template
    Select If(t = "-", "-", characters(rnd.Next(If(t = "?", characters.Length, 10))))

Dim result = String.Join("", query)

Console.WriteLine(result)

It gives me output like this:

RC2C9-DHB47-1Q07RL-8BIF7-57

9
  • I like your approach but if i'm not mistaken ??? gave 7RL, did you change the string before posting the output?
    – OctaCode
    Jun 3, 2019 at 9:37
  • @Devcon - Yes, the OP describes ? as outputting "a number or letter". Jun 3, 2019 at 9:48
  • oops i must use glasses i guess, thanks for your answer i can say i learned something today :)
    – OctaCode
    Jun 3, 2019 at 9:58
  • 1
    Great code, very simple and effective. I learned a couple things looking at it.
    – Mr.Alvaro
    Jun 3, 2019 at 16:15
  • Can you provide the project please so i may look at it thank you. Jun 4, 2019 at 10:37
0

Create 2 functions 1 for letters GRL (Generate Random Letter) 1 for numbers GRN (Generate Random Number) like so: Result of what i called is: W96-GKlF6

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Console.WriteLine(GRL(1) + GRN(2) + "-" + GRL(4) + GRN(1))
End Sub

Public Function GRL(ByRef iLength As Integer) As String
    Static rdm As New Random()
    Dim allowChrs() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ".ToCharArray()
    Dim sResult As String = String.Empty

    For i As Integer = 0 To iLength - 1
        sResult += allowChrs(rdm.Next(0, allowChrs.Length))
    Next

    Return sResult
End Function

Public Function GRN(ByRef iLength As Integer) As String
    Static rdm As New Random()
    Dim allowChrs() As Char = "0123456789".ToCharArray()
    Dim sResult As String = String.Empty

    For i As Integer = 0 To iLength - 1
        sResult += allowChrs(rdm.Next(0, allowChrs.Length))
    Next

    Return sResult
End Function
6
  • Can you provide the project please. Jun 3, 2019 at 8:44
  • how do you want that?
    – OctaCode
    Jun 3, 2019 at 8:51
  • well i uploaded that to wetransfer, please mark as answer if i helped :) we.tl/t-5N2D3CL1Ep
    – OctaCode
    Jun 3, 2019 at 9:10
  • How do i see the result, I just see an empty form when run. Jun 4, 2019 at 10:34
  • If you want a message, replace console.writeline with msgbox
    – OctaCode
    Jun 4, 2019 at 11:02
0

Easy, random numbers to use as ASCII codes, then check the position to delimit if its going to be just a number or a character that can be number or letter.

When is a position that can be number or letter, analyze the random number and split it. If the number is less than 11 that means is a number then add 47 and use the result as ASCII code (random create numbers from 1 to 36) so for example if the random is 1, we say 47 + 1 = 48, 48 is the ASCII code of 0.

If the number is 11 or more we add 54, so for example if random is 11 then we have 11 + 54 = 65. 65 is the ASCII code for the letter A.

    Dim Key As String = ""
    Dim N As Integer
    Randomize()
    For t = 1 To 23
        If t = 3 Or t = 5 Or t = 9 Or t = 10 Or t = 11 Or t = 13 Or t = 17 Or t >= 21 Then
            N = 10
        Else
            N = 36
        End If
        Dim value As Integer = CInt(Int(N * Rnd() + 1))
        If value < 11 Then
            Key = Key & Chr(value + 47)
        Else
            Key = Key & Chr(value + 54)
        End If
        If t = 5 Or t = 10 Or t = 16 Or t = 21 Then
            Key = Key & "-"
        End If
    Next
2
  • are you missing Randomize() function code or that for loop should be inside the function or what?
    – OctaCode
    Jun 3, 2019 at 9:15
  • I used before the loop start, but it can be moved inside the loop in the line after the For t...
    – Mr.Alvaro
    Jun 3, 2019 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.