0

So I have some C code which calculate some results based on the number generated by srand(). If I use the same seed number, the result will always be the same.

Now I have an Android app load these C code via JNI. However, the results become different although the same seed number is being used. I have double checked the seed number to make sure it is the same. However, since both the Android program and the native code are pretty complicated, I am having a hard time to figure out what is causing this problem.

What I am sure is, we did not use function in the java program to generate random numbers. So presumably srand() is not called with a different seed number every time. Can other functions in Java or C change the random number generated by srand()?

Thanks!

Update: I guess my question was a little confusing. To clarify, the results I am comparing are from the same platform, but different runs. The c code use rand() to get a number calculate a result based on that. So if the seed number of srand() is always the same, the number get by rand() should be the same and hence the results should be the same. but somehow even I use the same seed for srand(), the rand() give me different number... Any thought on that?

Bach
  • 45
  • 1
  • 6

2 Answers2

2

There are many different types of random number generators, and they are not all guaranteed to be the same from platform to platform. If having a cross platform 100% predictable solution is necessary for your project, you'll probably have to write your own.

It's really not as bad as it may sound...

I'd recommend looking up random number generation such as the Mersenne Twister algorithm (which is what I use in my projects), and write a small block of code that you can share amongst all your projects. This also gives you the benefit of being able to have multiple generators with varying seeds, which comes in really useful for something like a puzzle game, where you might want a predictably random set based on a specific seed to generate your puzzle, but another clock seeded generator for randomizing special FX or other game elements.

  • @Bach As far as your update goes... it is possible that there is another library or other section of code (not written by you) which is also calling `rand()` and causing your calls to get out of sync. This is yet another good reason to have your own, so that there are no collisions occurring from other areas of code. –  Aug 08 '13 at 03:51
  • Is it possible that some other function could change the internal buffer where Xi is stored? – Bach Aug 08 '13 at 15:54
  • @Bach `Xi` being your seed value? Anything is possible, yet the most likely cases are `srand()` being called by someone else after you call it (if you're seeing that even the very first number is different)... or `rand()` being called in another place which you aren't aware of, which would update the tables used to calculate the next random number, therefore giving you a different one. –  Aug 08 '13 at 16:03
  • @Bach Do a search for `srand()` and see if you find more than one occurrence in your code. –  Aug 08 '13 at 16:03
  • couldn't find any other places a srand() or rand() is called. I implemented a rand() function by myself and it works well. Thanks for all the suggestions! Still don't know what caused it though... – Bach Aug 08 '13 at 20:03
0

The pseudo-random algorithm implemented by rand() is determined by the C library, and there is no standard algorithm for it. You are absolutely not guaranteed to get the same sequence of numbers from one implementation to the next, and it sounds like the Android implementation differs from your development environment. If you need a predictable sequence across platforms, you should implement your own random number generator.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • Thanks for your answer, I have tested the C code on Android by creating a simple app to load the C code. I'm getting correct results out of it... don't know what is changing the rand() output... – Bach Aug 08 '13 at 15:42
  • Even if you've tested it and coincidentally found the same random number sequence on Android, you are taking a huge risk by relying on that. Any C library update could potentially change the algorithm that rand() uses. It's fine if you want to rely on that, but at least keep it in the back of your mind (and in some documentation somewhere) that it could very well come back to haunt you in the future. – Jason C Aug 08 '13 at 15:44