Log In  

Since working on compressing text in a simple bookreader:

https://www.lexaloffle.com/bbs/?tid=35608

I got curious and wanted to know if it was possible to recreate simple text that is chosen from random letters via the walking distance between the points when a match is made.

While I don't think this is at all useful for compression, it is interesting perhaps for use in encryption, secret messages, and your classic magic super decoder ring.

Actual code is really small, but it's chock full of helpful remarks.

Here it is:

-- secret decoder with random
-- written by dw817

-- simple wait for (o) key
function key()
  repeat
    flip()
  until btnp(4)
end

-- character set to use
char=" .abcdefghijklmnopqrstuvwxyz"

-- secret text, change to
-- your liking
t="the key is under the tree."

-- store the encrypted lookup
-- table here.
decod={}

cls()

-- force all random numbers to
-- follow this seed.
srand(1)

-- get length of string char.
-- we ask for it twice in the
-- code so this is convenient.
l=#char

-- starting position for lookup
-- table.
p=0

-- go through each character of
-- the secret string.
for i=1,#t do

-- pull out a single character.
  c=sub(t,i,i)

-- reset iterations needed to
-- find a match.
  n=0

  repeat

-- retrieve a random number to
-- use in finding a matching
-- character with our secret
-- string.
    r=flr(rnd()*l)+1

-- count the number of times it
-- takes.
    n+=1

-- keep looping until we get a
-- match.
  until c==sub(char,r,r)

-- match ? output the results.
  print("char="..c.." index="..i.." iterations="..n)

-- simple delay.
  flip()

-- record the time it took in
-- our lookup array.
  decod[p]=n

-- reset time.
  n=0

-- increase index for lookup
-- table.
  p+=1

-- do every single character
-- from secret string.
end

-- wait for (o) key.
key()

cls()
?"ready to decode:"

-- reseed our random number.
-- important !
srand(1)

-- reset position in lookup
-- table.
p=0

repeat

-- loop for length of lookup
-- table in elements.
  for i=1,decod[p] do

-- get a random number the
-- length of the chars used.  
    r=flr(rnd()*l)+1

-- keep looping the same number
-- of times it took to find
-- each character matching
-- initially.
  end

-- print out each newly
-- randomly generated
-- character.
  print(sub(char,r,r),p*4,64)

-- bit of a pause.
  flip()

-- increase position in lookup
-- table.
  p+=1

-- keep looping until we have
-- covered every character.
until p==#t

-- neat, print at bottom.
print("",0,120)

-- loop forever
repeat
  key()
until forever

The output to start with is the character we need to encode, the next is the index, the next is the time it took for RND() to randomly pick the correct matching letter.

After the "ready to decode:" then it just reseeds the same random seed and recreates the event based entirely upon the number of iterations needed to randomly pick the correct letters again.

Really something to see !

And it actually runs faster than this, too. Take out the FLIP() after each PRINT statement and it's off like a shot.

P#68725 2019-10-10 17:30 ( Edited 2019-10-10 18:19)


[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-11-17 19:57 | 0.010s | 4194k | Q:9