Seth Kitchen Seth Kitchen - 10 months ago 65
C# Question

How to use the same random seed between Windows Devices?

I am trying to shuffle cards in a deck using the same random seed so the decks will be random, but synced on both clients. I am using the following shuffle algorithm:

internal void ShuffleDeck(int randomSeed)
_random = new Random(randomSeed);
Cards.Card[] toShuffle = CardsInDeck.ToArray();
CardsInDeck = toShuffle.ToList<Cards.Card>();

/// <summary>
/// Shuffle the array.
/// </summary>
/// <typeparam name="T">Array element type.</typeparam>
/// <param name="array">Array to shuffle.</param>
private static void Shuffle<T>(T[] array)
int n = array.Length;
for (int i = 0; i < n; i++)
// NextDouble returns a random number between 0 and 1.
// ... It is equivalent to Math.random() in Java.
int r = i + (int)(_random.NextDouble() * (n - i));
T t = array[r];
array[r] = array[i];
array[i] = t;

When I run two instances of my card game on the same machine, the cards are shuffled and synced on both clients as expected, but when I run one instance on my computer and another in a HoloLens emulator, the cards use the same seed, but cards are not synced. Is there anyway to shuffle the cards and have them synced across multiple clients?

By Synced, I mean they are shuffled in the exact same way. IE when I run both clients the first time with four cards (a,b,c,d) the deck order is (b,c,a,d) on both clients. When I run the clients the second time the deck order is (c,d,a,b) on both clients.


In order to do this, you need to make sure that both machines are using the exact same seed and the same random number generator. There's no guarantee that different versions of the Random class will use the same algorithm.

So you need to develop your own random number generator class and use it instead of System.Random.