tags:

views:

31

answers:

1

Is it possible to "synchronize" (for lack of a better word) random number generators on two different machines?

Context: I have a card game for iOS with network multiplayer. Both peers need to be able to access the card deck. When the deck gets shuffled, I can serialize my card deck object with NSCoding and ship it over to the other peer so the decks are in sync, but I could send less data if I could just count on the random number generators on both peers to generate the same random numbers.

Because this is a card game, I need the best random numbers available, so I prefer to use arc4random(), which seeds itself when first called. Is there, perhaps, a way to seed it manually?

Simply sending the card deck object is an okay solution, but I plan to implement asynchronous multiplayer using OpenFeint, and to do that, my game states need to stay under 16K, and just my card deck is almost 2K when serialized. :)

Thanks!

A: 

What if you just sent over the random number itself? Whichever device's "turn" it is can have the task of creating and sending it's random number so that all the versions can acknowledge the same draw. If you're careful to make sure that all the versions of the deck are in the same order/cards and that no random number is dropped, it should theoretically work with a very small amount of data transfer. If another player comes in part way through, you can mark which cards have been drawn by flipping individual bits in an object or array of objects and send that object/array.

Failing that I'm afraid I don't know the platform, but I do know that in some other languages, putting a seed as part of the call will seed it with a fixed value (ie: arc4random(seed)) I don't know if this is the case on what you're working on, but it's probably worth looking into.

Hope this helps!

Lunin