Please help support this free site and future BRL projects!

Login or Signup

Random Seed behaviour across OS inconsistent?

BlitzPlus Forums/BlitzPlus Programming/Random Seed behaviour across OS inconsistent?

morduun(Posted 1+ years ago) #1
Ran across an odd little observation today and I'm trying to work out whether it's my code that's messed or something else entirely.

Basically, I'm trying to share information between a client and a server. I'm attempting to recreate a series of random numbers between said client and server. I'm using BlitzPlus to do this.

When I do this between said client and server, both using WinXP, the series is recreated successfully -- this even works properly across separate physical computers. When I do this between client and server on WinME, it works fine as well. However, when I attempt to get a client on XP and a server on ME talking -- or vice versa -- the random sequence using the same seed is buggered up and won't replicate properly.

Is this expected behavior? If so, is there any way around this? Again, this is using precisely the same version of Blitz+, just on different OS's.

MadMax(Posted 1+ years ago) #2
Don't think it has anything to do with your code, not only different OS, different PC's may very well give different results. Maybe a way round that, would be that only one of the PC's generates the data and sends it to the other one(s).

morduun(Posted 1+ years ago) #3
One PC generating the sequence and transmitting it defeats the purpose entirely, unfortunately.

If this is expected behaviour I'll have to gen my own, consistent randomization procedure based on seeds. Ugly, but if that's expected behaviour I've no other choice. :/

Zenith(Posted 1+ years ago) #4
Why the hell would random output be different on one computer, and then different on the next -- that's not possible with how a random generator works, the answer should be the same on every computer.

This is deffinatly a Blitz issue.

Anthony Flack(Posted 1+ years ago) #5

Why the hell would random output be different on one computer, and then different on the next

I'm taking a stab in the dark here, knowing nothing about which I speak (what else is new?) but I'm gonna go with - slightly different degrees of accuracy in floating point maths? Or am I an idiot?

morduun(Posted 1+ years ago) #6
In the reading I've been doing on the subject (in order to prepare myself in case I need to write one) a consistent random seeding procedure is necessary and commonly written for the same reasons I'm using it -- namely for cryptography and security. The fact that I'm getting unusual results is very strange indeed if that's the case, as these algorithms seem to be written specifically with an eye towards consistency and portability.

MutteringGoblin(Posted 1+ years ago) #7

I'm assuming you have a C compiler lurking around somewhere? If you do then you already have the source for a random number generator. Just find the code for rseed() and rand() and translate it into Blitz. That's what I would do. After all, the standard libraries are yours to do what you like with.

morduun(Posted 1+ years ago) #8
MG --

Yes, I can do that without difficulty; it just seems a shame to have to do it longhand when one would logically assume that Blitz's internals would otherwise handle this with aplomb. Some sort of verification that this is either (A) expected behavior and therefore I should trudge ahead with porting the code, or (B) no, this is a bug and will be fixed in a patch, would be nice -- not just for me, but for anyone else who's expecting Blitz's random number generation systems to be consistent.

marksibly(Posted 1+ years ago) #9

Random sequences should be identical between 2 machines.

Have you tried just printing a sequence of 10 numbers on both OS's and comparing results? Can you dump them here?

If you're trying to do what I think you are (ie: produce identical game results based on identical random numbers and user input - ie: 'demo playback') its pretty hard, but good luck!

morduun(Posted 1+ years ago) #10
I'd hoped you'd say that, though it looks like that might mean there's a sneaky little sucker in there somewhere. ;)

10-number sequences from XP and ME follow, and speak for themselves; they're very similar but not identical.


SeedRnd 723122303

myFile = WriteFile("XPrand.txt")

For i = 1 To 10
	x = Rand(0, 2147483647)
	WriteLine myFile, x

CloseFile myfile


P3/1G XP results:


P3/700 ME results:


VERY similar -- but not the same.

skidracer(Posted 1+ years ago) #11
nice bug hunting dude!

Mark, see the following for possible cause:

morduun(Posted 1+ years ago) #12
Thanks Simon, though I'm the idiot that didn't think to just do a straight run of 10 random numbers to see the difference for myself until Mark asked for it. ;) Just nice to know that I'm not going =totally= insane. Or, er, at least not any more so than usual.

Anthony Flack(Posted 1+ years ago) #13
Don't tell me my theory was actually RIGHT?!? I think I need to go have a lie down...

morduun(Posted 1+ years ago) #14
*sigh* Apparently this fix didn't make it into the 1.34 update, so there's one more project frozen in place. :/ Mark, any idea when this'll be addressed?

marksibly(Posted 1+ years ago) #15

We're looking into this one, but its a fairly complex issue...

semar(Posted 1+ years ago) #16
I *think* this has something to do with float and/or real numbers, which differ on different machines because of float approximation.

The workaround would be: use small random numbers !

If you see in the above list of numbers, the first 6 digit are all syncronized between the two lists, while the rest differ.

This can have something to do with approximation, IMO.

My Asteroids remake is also a demo playback (there are 3 different demos saved in .txt plan files), and works fine on all the pc I have tested so far.

It uses indeed random numbers which are generated using the same seed, but the numbers I use for the seed are small, I mean, not with 9 digits !!

So, my advice is: use small seeds !



morduun(Posted 1+ years ago) #17
Not an option when you're using the random number generator for a cryptographic application, semar, but thanks for the thought. ;) I'd roll my own but one needs doubles to do it properly -- so for now I'm pretty much stuck.

Koriolis(Posted 1+ years ago) #18
I had always thought the algorithms for pseudo random number generation all use integers and never floats precisely to avoid such problems. If the built in blitz one uses floats then it's completly flawed from the very beginning (I would be very surprised if it was, a word of Mark about this wouls be interesting). The (few) algorithms I've seen use plain integers, and I guess generators for float numbers should be derived from these ones rather than using floats for their computations.

Koriolis(Posted 1+ years ago) #19
I just found a pieace of article showing what I thought:
The real (float) version of a random generator (returns a float in the [0,1) or [0,1] range) is here:
Rn = In/ float(m) for [0,1)
Rn = In/ float(m-1) for [0,1]
where m is the modulo for the method and In the integer random number.
The division is the only floating point operation in all the process, so using this, for float random numbers the precision errors are really not a big issue, anf for integers it's not at all because then there's only integer operations.

So the wierdest thing is morduun showed little differences with INTEGER random numbers. Mark, any comments?

cyberseth(Posted 1+ years ago) #20
Come on, morduun, you can't seriously say that Blitz not being able to have a (non)random number generator freezes your project! Just code your own for the time-being! Create your own functions for Rnd(), Rand() and SeedRnd() and use DATA statements to get the numbers you want.

Then, when Blitz+ is updated to reflect the whole "random number" issue, you can just comment out your functions and voila!

morduun(Posted 1+ years ago) #21
Seth, I'm not interested in a workaround, thanks. Mark's agreed it's broken and is looking into it. I have no interest in wasting time writing redundant functions that will end up being unused.

cyberseth(Posted 1+ years ago) #22
What, you never use placeholders?

SDF_of_BC(Posted 1+ years ago) #23
could you use a formula instead that gives close to random results like one that goes into chaos!

morduun(Posted 1+ years ago) #24
What, Seth, you never evaluate functionality to determine whether it will properly fulfill your needs or not? You simply press ahead and assume that the direction you've chosen to take will work regardless of whether functionality will support the application fully no matter how deeply integrated into said application the questionable functionality is?

Perhaps you enjoy wasting your time doing such things. I do not. If this functionality cannot be supported I will have to rethink a significant portion of the application's underpinnings and I have no desire to press ahead simply assuming that it will be fixed.

It's a complex problem. Mark understands it's important to me. I understand he's doing his best. 'Nuff said.

cyberseth(Posted 1+ years ago) #25
Well er heh as long as one enjoys wasting their time, then it's time well-spent! ;) Besides, I didn't say that I would press on using the flawed functionality, but rather than halting the entire production line for the sake of a loose pin I'd probably jam something in there to keep it going till I find that darn pin again..

Sounds questionable I agree, particularly with the given analogy, but I just didn't want you to have to halt your app simply for the sake of a random seed generator, and was providing alternatives to fill in until said seed generator becomes available.

morduun(Posted 1+ years ago) #26
Appreciate the sentiment Seth, and there =are= certain things that can (and are) move(ing) ahead a little, but cryptography is an intrinsic part of the app and I don't want to move ahead too much without making a firm decision on an approach I can go forward with. Recoding is my second-least favorite thing to do in programming (bughunting being my first-least favorite), and I'd rather set something aside for a while than spend time on bits that will just end up in the circular file.