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 


How do I make a better Random Number Generator?

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

Joined: 09 Jun 2015
Posts: 77

PostPosted: Sat Aug 27, 2016 12:19 pm    Post subject: How do I make a better Random Number Generator? Reply with quote

Hello guys. So, a trainer I'm making checks the OS time and then based on that writes a random value to several addresses. The only problem is is the random values generated for each are all the same. I did some reading, and it's because Lua doesn't measure OS time in milliseconds, only seconds. This is my code as of right now:

Code:

  math.randomseed( os.time() )

  if (intatk1Byte1 == 0) then
    intatk1Byte2 = math.random(1, 255)
  else
    intatk1Byte2 = math.random(0, 117)
  end

   if (intatk2Byte1 == 0) then
    intatk2Byte2 = math.random(1, 255)
  else
    intatk2Byte2 = math.random(0, 117)
  end

  if (intatk3Byte1 == 0) then
    intatk3Byte2 = math.random(1, 255)
  else
    intatk3Byte2 = math.random(0, 117)
  end

  if (intatk4Byte1 == 0) then
    intatk4Byte2 = math.random(1, 255)
  else
    intatk4Byte2 = math.random(0, 117)
  end


Is there any way I could wait a second between generating the random numbers above so I get a different result? Or better yet, get OS.time to interpret milliseconds so it can do the work within milliseconds(as I need to constantly write these random values to said addresses) Thanks in advanced![/code]
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sat Aug 27, 2016 12:38 pm    Post subject: This post has 1 review(s) Reply with quote

Code:
myvar = myvar and myvar < 1000 and myvar or 0
math.randomseed( os.time() * 1000 + myvar )
myvar = myvar + 1
Back to top
View user's profile Send private message
Reclaimer Shawn
Advanced Cheater
Reputation: 0

Joined: 09 Jun 2015
Posts: 77

PostPosted: Sat Aug 27, 2016 1:42 pm    Post subject: Reply with quote

Zanzer wrote:
Code:
myvar = myvar and myvar < 1000 and myvar or 0
math.randomseed( os.time() * 1000 + myvar )
myvar = myvar + 1


This is how I included your code:

Code:

function random()
  math.randomseed( os.time() )
  inta = readInteger("8049901C")
  intspeciesByte1 = math.random(0, 1)
  intspeciesByte2 = 0
  intatk1Byte1 = math.random(0, 1)
  intatk1Byte2 = 1
  intatk2Byte1 = math.random(0, 1)
  intatk2Byte2 = 2
  intatk3Byte1 = math.random(0, 1)
  intatk3Byte2 = 3
  intatk4Byte1 = math.random(0, 1)
  intatk4Byte2 = 4

  if (intspeciesByte1 == 0) then
    intspeciesByte2 = math.random(1, 251)
  else
    intspeciesByte2 = math.random(21, 155)
  end

  if (intatk1Byte1 == 0) then
    myvar = myvar and myvar < 1000 and myvar or 0
    math.randomseed( os.time() * 1000 + myvar )
    myvar = myvar + 1
    intatk1Byte2 = math.random(1, 255)
  else
    myvar = myvar and myvar < 1000 and myvar or 0
    math.randomseed( os.time() * 1000 + myvar )
    myvar = myvar + 1
    intatk1Byte2 = math.random(0, 117)
  end

   if (intatk2Byte1 == 0) then
    myvar = myvar and myvar < 1000 and myvar or 0
    math.randomseed( os.time() * 1000 + myvar )
    myvar = myvar + 1
    intatk2Byte2 = math.random(1, 255)
  else
    myvar = myvar and myvar < 1000 and myvar or 0
    math.randomseed( os.time() * 1000 + myvar )
    myvar = myvar + 1
    intatk2Byte2 = math.random(0, 117)
  end

  if (intatk3Byte1 == 0) then
    myvar = myvar and myvar < 1000 and myvar or 0
    math.randomseed( os.time() * 1000 + myvar )
    myvar = myvar + 1
    intatk3Byte2 = math.random(1, 255)
  else
    myvar = myvar and myvar < 1000 and myvar or 0
    math.randomseed( os.time() * 1000 + myvar )
    myvar = myvar + 1
    intatk3Byte2 = math.random(0, 117)
  end

  if (intatk4Byte1 == 0) then
    myvar = myvar and myvar < 1000 and myvar or 0
    math.randomseed( os.time() * 1000 + myvar )
    myvar = myvar + 1
    intatk4Byte2 = math.random(1, 255)
  else
    myvar = myvar and myvar < 1000 and myvar or 0
    math.randomseed( os.time() * 1000 + myvar )
    myvar = myvar + 1
    intatk4Byte2 = math.random(0, 117)
  end


I feel I didn't know how to use the code you provided as the values of said code are either always the same or 1 number bigger. The code looks simple enough, but could you provide me with an example on how to use it? Sorry for all of the trouble, just not used to generating random numbers within CE.
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 942

PostPosted: Sat Aug 27, 2016 2:54 pm    Post subject: Reply with quote

This a high resolution counter using windows api QueryPerformanceCounter, for ce 6.5+
Code:
function qpc(calltype)
  if not readIntegerLocal('_qpc') then
    local ht = cheatEngineIs64Bit() and {'mov rcx,_qpc','ret'} or {'push _qpc','ret 04'}
    autoAssemble(table.concat({
    'globalalloc(_qpc,$100)',
    '_qpc+20:',
      ht[1],
      'call QueryPerformanceCounter',
      ht[2],
    '_qpc+80:',
      ht[1],
      'call QueryPerformanceFrequency',
      ht[2]
    },'\n'),true)
  end
  local call = type(calltype)=='string' and calltype:lower():sub(1,1)=='f' and '_qpc+80' or '_qpc+20'
  if executeCodeLocal(call) ~= 0 then
    return readQwordLocal('_qpc')
  end
end


It return the cpu Ticks since os up.
qpc('f') to get the QueryPerformanceFrequency, ticks per second, which can be used to convert the ticks to seconds.

Hopefully, replacing os.time() with qpc() should serve the purpose.

bye~

_________________
- Retarded.
Back to top
View user's profile Send private message
kuntz
Cheater
Reputation: 0

Joined: 29 Aug 2016
Posts: 44
Location: Canada

PostPosted: Tue Aug 30, 2016 5:00 pm    Post subject: Reply with quote

What is the over-all goal of the code you are trying to produce? There is probably a very easy solution!

  • How often is this code called? Once per frame? Once per second?
  • Are you able to save a single variable between script-executions to hold the seed?
  • Is the reason you are using time to seed the PRNG because you cannot store a value? (That is the only reason to do this)
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