Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Random Executing()

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
MateeJr GT
Advanced Cheater
Reputation: 0

Joined: 24 Dec 2017
Posts: 66

PostPosted: Sun Jun 03, 2018 9:15 pm    Post subject: Random Executing() Reply with quote

Executing / Generating random Text
_________________
Hi Lynxz Gaming
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Mon Jun 04, 2018 1:46 pm    Post subject: Reply with quote

Code:
local function randomAlpha() return string.char(math.random(('a'):byte(), ('z'):byte())) end
local function randomText(len)
  if not len or type(len) ~= 'number' or len < 1 then error('Invalid len to randomText', 2) end
  local res = {}
  for i = 0, len do res[#res+1] = randomAlpha() end
  return table.concat(res)
end

print(randomText(241)) -- print 241 random letters

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Mon Jun 04, 2018 5:54 pm    Post subject: Reply with quote

or

Code:
local function RandomText( len )
   str = ""
   for i = 0, len do
      str = str .. string.char( math.random(97,122) )
   end
   return str
end

print(RandomText(241))

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Mon Jun 04, 2018 6:00 pm    Post subject: Reply with quote

yeah, but iirc lua strings are immutable and concatenating like that is kind of bad memory wise Smile

concatenating 1 character to a 200 character string has to allocate a 201 character string, copy 200 characters then add the new one, adding another allocates a new 201 characters repeat Smile

The following measuring code results in
FreeER 5.2978515625
Corroder 37.6201171875

at 1000
FreeER 18.0390625
Corroder 523.8544921875

increase the length to 10000 and you get

FreeER 282.8828125
Corroder 49175.725585938

at 100000
FreeER 2386.890625
Corroder 4886280.2177734
with a very noticeable delay between the prints...

note the values are in kilobytes so that makes the final results for a 100000 character random string 2.33 MB vs 4.66 GB.

Code:
local function measure(func)
  collectgarbage("collect")
  collectgarbage("stop")
  thestart = collectgarbage("count")
  pcall(func)
  theend = collectgarbage("count")
  collectgarbage("restart")
  return theend - thestart
end

local function RandomString( len )
   str = ""
   for i = 0, len do
      str = str .. string.char( math.random(97,122) )
   end
   return str
end

local function randomAlpha() return string.char(math.random(('a'):byte(), ('z'):byte())) end
local function randomText(len)
  if not len or type(len) ~= 'number' or len < 1 then error('Invalid len to randomText', 2) end
  local res = {}
  for i = 0, len do res[#res+1] = randomAlpha() end
  return table.concat(res)
end

local len = 10000
print('FreeER  ', measure(function() randomText(len) end))
print('Corroder', measure(function() RandomString(len) end))

_________________
https://github.com/FreeER/ has a few CE related repos


Last edited by FreeER on Mon Jun 04, 2018 6:21 pm; edited 4 times in total
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Mon Jun 04, 2018 6:08 pm    Post subject: Reply with quote

Quote:
concatenating like that is kind of bad memory wise
Laughing

yeah you right...

And this one below, from somewhere on github.

Code:
local charset = {}  do      --   [0-9a-zA-Z]
    for c = 48, 57  do table.insert(charset, string.char(c)) end
    for c = 65, 90  do table.insert(charset, string.char(c)) end
    for c = 97, 122 do table.insert(charset, string.char(c)) end
end

local function randomText(length)
    if not length or length <= 0 then return '' end
    math.randomseed(os.clock()^5)
    return randomText(length - 1) .. charset[math.random(1, #charset)]
end

print(randomText(241))

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Mon Jun 04, 2018 6:24 pm    Post subject: Reply with quote

That one is a recursive concatenation (and no tail-call because concatenation) so I can only imagine it'd be worse both in memory and cpu usage even with the lookup table Laughing

Oh, not to mention re-seeding n times Laughing

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites