Go Back   Access World Forums > Microsoft Access Reference > Code Repository

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 11-21-2019, 07:26 AM   #1
Guus2005
AWF VIP
 
Guus2005's Avatar
 
Join Date: Jun 2007
Location: The Netherlands
Posts: 2,529
Thanks: 54
Thanked 87 Times in 80 Posts
Guus2005 is a jewel in the rough Guus2005 is a jewel in the rough Guus2005 is a jewel in the rough
Scramble Text

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 by Guus2005; 11-29-2019 at 12:16 AM. Reason: Added Randomnes and a H2G2 reference
Guus2005 is offline   Reply With Quote
The Following User Says Thank You to Guus2005 For This Useful Post:
sonic8 (11-28-2019)
Old 11-21-2019, 11:48 AM   #2
Galaxiom
Super Moderator
 
Join Date: Jan 2009
Location: NSW Australia
Posts: 11,712
Thanks: 105
Thanked 1,511 Times in 1,424 Posts
Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold
Re: Scramble Text

That function will not make the data unrecoverable.

Rnd() returns a pseudorandom sequence. The same sequence is used unless you run Randomize to seed the function with a value derived from the system timer before calling Rnd().

You need to repeatedly call Ranndomize or the data may be able to be descrambled based on the characteristics of the sequence.

https://docs.microsoft.com/en-us/off...p/rnd-function
Galaxiom is offline   Reply With Quote
Old 11-21-2019, 05:11 PM   #3
Micron
AWF VIP
 
Join Date: Oct 2018
Location: Ontario, Canada
Posts: 1,229
Thanks: 10
Thanked 231 Times in 219 Posts
Micron has a spectacular aura about Micron has a spectacular aura about
Re: Scramble Text

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?

Micron is offline   Reply With Quote
Old 11-26-2019, 05:43 PM   #4
Micron
AWF VIP
 
Join Date: Oct 2018
Location: Ontario, Canada
Posts: 1,229
Thanks: 10
Thanked 231 Times in 219 Posts
Micron has a spectacular aura about Micron has a spectacular aura about
Re: Scramble Text

Quote:
Originally Posted by Guus2005 View Post
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.
__________________
Sometimes I just roll my eyes out loud...
Windows 10; Office 365 (Access 2016)
Micron is offline   Reply With Quote
Old 11-26-2019, 05:55 PM   #5
MajP
Newly Registered User
 
Join Date: May 2018
Location: USA baby
Posts: 1,888
Thanks: 38
Thanked 571 Times in 538 Posts
MajP has a spectacular aura about MajP has a spectacular aura about
Re: Scramble Text

Quote:
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.
MajP is offline   Reply With Quote
Old 11-27-2019, 02:14 PM   #6
MajP
Newly Registered User
 
Join Date: May 2018
Location: USA baby
Posts: 1,888
Thanks: 38
Thanked 571 Times in 538 Posts
MajP has a spectacular aura about MajP has a spectacular aura about
Re: Scramble Text

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.
Attached Files
File Type: accdb CompareRandomize.accdb (572.0 KB, 4 views)

MajP is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question importing multiple rtf documents into rich text format long text field. niawo General 2 07-05-2017 02:50 AM
[SOLVED] How to scramble words with random characters ? smig Modules & VBA 12 12-23-2016 11:37 PM
Transferring Text From Text Box To Email, Can I Replace Line Breaks With Commas? the-m0th Modules & VBA 1 05-04-2016 06:28 AM
Suggestion please- Scramble characters MCantu Modules & VBA 2 06-07-2007 07:54 AM
A query to SELECT a columen within an interval which conatains TEXT+DIGIT+TEXT kia70 Queries 1 12-28-2006 10:55 PM




All times are GMT -8. The time now is 02:34 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World