Generate random string in text field
Asked Answered
C

3

6

We have that old software (made by one of the first employees many years ago) in company that uses Microsoft Access to run. Boss asked me to add a random string generation in the specific text box on click but i have no idea how to do that. I dont have any Microsoft Access programming experience, thats why i am askin you to help.

I managed to create button and text field so far. Thats where it stops. I also managed to access the code for the button action:

Private Sub command133_Click()

End Sub
Cantata answered 25/3, 2014 at 9:28 Comment(5)
@Bathsheba letters and numbers, lets say 8 characters longCantata
do you want lower and upper case letters? The answer I've given is for upper case letters only.Lumbye
@Lumbye Yes also lower case letters + numbers if it is not too much work :)Cantata
I've amended: generates letters an numbers nowLumbye
@Lumbye Thank you :) Will try to understand how it works and learn something new, not just copy it.Cantata
L
11

This is one way, will work in Access VBA (which is an older basic than vb.net). It will generate a string with letters and numbers.

Sub test()

    Dim s As String * 8 'fixed length string with 8 characters
    Dim n As Integer
    Dim ch As Integer 'the character
    For n = 1 To Len(s) 'don't hardcode the length twice
        Do
            ch = Rnd() * 127 'This could be more efficient.
            '48 is '0', 57 is '9', 65 is 'A', 90 is 'Z', 97 is 'a', 122 is 'z'.
        Loop While ch < 48 Or ch > 57 And ch < 65 Or ch > 90 And ch < 97 Or ch > 122
        Mid(s, n, 1) = Chr(ch) 'bit more efficient than concatenation
    Next

    Debug.Print s

End Sub
Lumbye answered 25/3, 2014 at 9:40 Comment(4)
+1, as an alternative, I would use additional rand value to determine from which range genereate ch. Idea is from here: https://mcmap.net/q/22176/-randbetween-1-11-but-not-6/…Chappell
Hi @Bathsheba, just a feedback. I test your code in Excel for random string 13 characters length. in 1million rows, excel create 13,826 duplicated strings and only 986,174 unique string left. It may be unique enough in most case, but I think there is a flaw in Rnd() function. In theory, a randomly select of 13 characters & number in group of 62 characters and number (0-9 a-z A-Z), there should be more than 8.3 billion unique selections. So 14k duplicate records in 1million records is quite significant. Any though? Thanks For your code.Vidavidal
Uniquely random is an entirely different requirement. You are correct, the periodicity of Rnd() is remarkably small.Lumbye
Maybe you should add Randomize before the call to Rnd()Cecilacecile
L
1

Try this function:

Public Function GetRandomString(ByVal iLength As Integer) As String
    Dim sResult As String = ""
    Dim rdm As New Random()

    For i As Integer = 1 To iLength
        sResult &= ChrW(rdm.Next(32, 126))
    Next

    Return sResult
End Function
Lammond answered 25/3, 2014 at 9:36 Comment(2)
Q about Access VBA, but not VB.NETChappell
Yes, but the tags were edited after this answer was submitted. It would work in vb.net, so +1.Lumbye
R
1

Workin on @Bathsheba code, I did this. It will generate a random string with the number of characters you'd like.

Code :

Public Function GenerateUniqueSequence(numberOfCharacters As Integer) As String

    Dim random As String  ' * 8 'fixed length string with 8 characters
    Dim j As Integer
    Dim ch As Integer   ' each character

    random = ""

    For j = 1 To numberOfCharacters
        random = random & GenerateRandomAlphaNumericCharacter
    Next

    GenerateUniqueSequence = random

End Function

Public Function GenerateRandomAlphaNumericCharacter() As String

    'Numbers : 48 is '0', 57  is '9'
    'LETTERS : 65 is 'A', 90  is 'Z'
    'letters : 97 is 'a', 122 is 'z'

    GenerateRandomAlphaNumericCharacter = ""

    Dim i As Integer

    Randomize
    i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories

    Randomize
    Select Case i
        Case 1 'Numbers
            GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 9 + 48)
        Case 2 'LETTERS
            GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 65)
        Case 3 'letters
            GenerateRandomAlphaNumericCharacter = Chr(Rnd() * 25 + 97)
    End Select

End Function

I use it with random number of characters, like this :

'Generates random Session ID between 15 and 30 alphanumeric characters
SessionID = GenerateUniqueSequence(Rnd * 15 + 15)

Result :

s8a8qWOmoDvC4jKRjPr5hOY12u 26
TB24qZ4cNfr6EdyY0J 18
6LZRQ9P5WHLNd71LIdqJ 20
KPN0RmlhhJKnVzPTkW 18
R2pNOKWJMKl9KpSoIV2egUNTEb1QC2 30
X8jHuupP6SvEI8Dt2wJi 20

NOTE: This is still not completely random. It will give a higher count of numbers than normal as approx 1/3 of all chars generated will be numbers.

Normally distribution will look like: 10 numbers plus 26 lowercase plus 26 uppercase = 62 possible chars. Numbers will normally be 10/62 parts of the string or 1/6.2
With the code i = (Rnd() * 2) + 1 'One chance out of 3 to choose one of 3 catégories the count of numbers is pushed up to 1/3 (on average)

Probably not too much of a worry - unless you are trying to beat the NSA and then you have decreased your range significantly.

Rowles answered 21/6, 2017 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.