1

When studying the first programming course at college we learnt that time is introduced as the seed value of the rand function in order to give out random values every time the code runs. If I can fix time and play a game that gives random levels each time you hit play, will I always get the same level? And if yes is there anyway to do this?

Peter Emil
  • 573
  • 3
  • 13
  • 1
    Don't use the time as the seed. Call `srand()` with a specific seed instead of the time. – Barmar May 20 '15 at 22:10
  • Yes I am asking about using this with a specific game that I play, I want to always get the same level, so is this feasible or not? – Peter Emil May 20 '15 at 22:12
  • If you're the system administrator you can set the clock on the system and then run your game. You should do it in a script so there's no typing delay between them. – Barmar May 20 '15 at 22:15
  • What operating system? The details of doing this are OS-specific. – Barmar May 20 '15 at 22:16
  • 1
    @Barmar - that's likely not enough.. you need to set the time at the exact time when the PRNG is seeded. Otherwise there could be other processes that could introduce delays – Mike Dinescu May 20 '15 at 22:17
  • I know, but it's about as good as you can do without modifying the program. – Barmar May 20 '15 at 22:18
  • 1
    Surely this only works if the resolution of the clock used by the software is in the seconds' range? (related: http://stackoverflow.com/q/5574914/2564301, and dozens of similar questions) For example, if the load time of this application is more than a second, it won't work with setting the clock. – Jongware May 20 '15 at 22:19
  • I would like to know if I can do this on windows 7 and jailbroken iOS – Peter Emil May 20 '15 at 22:19
  • @Jongware a second seems like a reasonable margin of error, you can always set the clock back an extra 3 seconds if the app takes 3.4--3.7 seconds to load but finding what second is used to seed during the load could take a few tries. – ryanpattison May 20 '15 at 22:23
  • 1
    Most games are smart enough to add "user Input" to the timing of the start of the game as well as a very precise timer to seed with. This makes sure this cannot be exploited trivially. Don't seed on start, but after the first or second user input. – Michael Dorgan May 20 '15 at 22:27

2 Answers2

1

If the game uses a pseudo-random number generator that is seeded from the runtime timestamp then yes, if you manage to set the time to the same value each time the game is started then you should get the same levels.

Probably the way to do it though would be to intercept the calls to the get system time system call and set the time to a specific value at that time and let the rest of it go unaltered.

You could try to give it a go with IDA Pro (https://www.hex-rays.com/products/ida/) or some other disassembler/debugger. I also found this book an interesting read with respect to hacking with IDA Pro (http://www.amazon.com/Reversing-Secrets-Engineering-Eldad-Eilam/dp/0764574817)

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • Thank you, can you give me a link or at least tell me what to search for in order to learn more about how to do this? – Peter Emil May 20 '15 at 22:16
  • On Unix you could use `LD_PRELOAD` to replace the `time()` function. – Barmar May 20 '15 at 22:16
  • @PeterEmil I've added two links - one to the IDAPro website and the other to a book that is written like a tutorial that explains how to use tools like IDA and WinDBG to identify the code you would like to intercept and how to modify it to do what you want – Mike Dinescu May 20 '15 at 22:24
  • 1
    Or just pass in a set value instead of using time. Then, you completely control the state of the game. – Michael Dorgan May 20 '15 at 22:25
  • @MichaelDorgan: yup - modifying the original executable. Rigging the system to always return the same time may have been possible in ye olde DOS days but under Windows I'm not so sure that's doable. – Jongware May 20 '15 at 22:30
  • If you know where the call is, you probably could hit the word. Of course for games that live on a server and you access as a client, you get booted for stuff like this. :) – Michael Dorgan May 20 '15 at 22:31
0

I suspect you are not likely to be able to freeze time with sufficient granulity, unless the game was coded to use seconds rather then msec or usec. Ask it will take is one hardware interrupt from mouse, disk, display network etc to make this non deterministic.

Don't cheat.... ;-)

Ron Kuper
  • 817
  • 5
  • 14