Scramble Text

Status
Not open for further replies.

Guus2005

AWF VIP
Local time
Today, 10:51
Joined
Jun 26, 2007
Messages
2,642
This function replaces every character with a random similar character.
I use this function to anonymize data in tables.

Almost but not quite entirely unlike the post made by Micron a few months ago.

Share & Enjoy!

Code:
Option Compare Binary
Option Explicit

Public Function ScrambleIt(strString As String) As String
    'Replace matching case and numbers
    'ScrambleIt("Kerkstraat 45, 8194AZ, Baarn")
    '           "Gibmfwptyi 50, 6491YQ, Jdmfz"
    Const cUAlpha As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Const cLAlpha As String = "abcdefghijklmnopqrstuvwxyz"
    Const cNum As String = "0123456789"
    Dim lngLoop As Long
    Dim strNewChar As String
    Dim strThisChar As String

    Randomize
    
    For lngLoop = 1 To Len(strString)
        strThisChar = Mid$(strString, lngLoop, 1)
        Select Case strThisChar
            Case "a" To "z"
                strNewChar = Mid$(cLAlpha, Int(Rnd * Len(cLAlpha)) + 1, 1)
                If Asc(strThisChar) > Asc("z") Then strNewChar = Asc("a") + (Asc(strThisChar) - Asc("a") - 1)
            Case "A" To "Z"
                strNewChar = Mid$(cUAlpha, Int(Rnd * Len(cUAlpha)) + 1, 1)
                If Asc(strThisChar) > Asc("Z") Then strNewChar = Asc("A") + (Asc(strThisChar) - Asc("A") - 1)
            Case "0" To "9"
                strNewChar = Mid$(cNum, Int(Rnd * Len(cNum)) + 1, 1)
                If Asc(strThisChar) > Asc("9") Then strNewChar = Asc("0") + (Asc(strThisChar) - Asc("0") - 1)
            Case Else
                strNewChar = strThisChar
        End Select
        ScrambleIt = ScrambleIt & strNewChar
    Next
End Function
 
Last edited:
It's pseudo random because each number seeds the generator for the next number, yes? If this were about a system picking winning lottery numbers, then I'd agree that rnd by itself is insufficient. However, there is no way (that I know of) that you're going to repeat the exact sequence when the function is run again, even if it were to start with the same number. If you say different, how about if I scramble some data and you unscramble it?
 
Due to the pseudo randomness (which could be easily solved) and the fact that i missed an earlier post which Colin pointed out, i think this post could be deleted since it adds nothing much :(
If I understand that statement, you're suggesting that the randomness factor could be improved by implementing the suggestion(s) at the link you posted. I have some requests/suggestions on the back burner that I have to review and I don't see why that one can't be added. At the very least, it ought to alleviate any such concerns, although I doubt that the results can be reversed as it stands.
 
If this were about a system picking winning lottery numbers, then I'd agree that rnd by itself is insufficient. However, there is no way (that I know of) that you're going to repeat the exact sequence when the function is run again, even if it were to start with the same number.
No you are wrong. It is not a bug, but purposely designed this way. If you do an experiment you want to get a unique set of numbers, but you would like to be able to run it again with the same unique set of numbers. So if you do not randomize or modify the see and close the db, then rerun it you will get that same unique sequence of numbers. I have demonstrated this on numerous threads. So if you are not uniquely seeding then you need to randomize. The term pseudo random has nothing to do with the seed, but with the algorithm. AFAIK no one has developed a truly random number generator via a pure code algorithm, they are really really good but not truly random.
https://www.random.org/randomness/
There are systems to pull truly random numbers.
 
Here is the demo showing randomize and not randomize. If you open the form you will see I list the order of the random IDs returned for the first three requeries. The list on the left employs does not use randomize, the one on the right does.
 

Attachments

Status
Not open for further replies.

Users who are viewing this thread

Back
Top Bottom