Schnopsn: Card Shuffling Algorithm

Randomness is something that is often counter-intuitive and individual perception of random series, for example a deck of cards, might sometimes give the impression that there is some kind of „intelligence“ or plan behind it.

After various user requests, on how the card shuffling for Schnopsn works, and if it is really random we decided to publish our simple, but effective shuffling algorithm here … and yes, the card distribution is totally random, we do not interfere with it in any case.

Our algorithm is based on the following pseudocode, suggested in this article: http://www.cigital.com/papers/download/developer_gambling.php


START WITH FRESH DECK
GET RANDOM SEED
FOR CT = 1, WHILE CT <= 52, DO
X = RANDOM NUMBER BETWEEN CT AND 52 INCLUSIVE
SWAP DECK[CT] WITH DECK[X]

And here is our Java Implemenation of the code as it is 1:1 used on the server side of Schnopsn Online as well as in the Singleplayer mode of the Android App. We are aware that we use the current millisecond as random seed, but that should be perfectly fine for our purpose.

	//	START WITH FRESH DECK
	ArrayList cards = getFullDeck();

        //	GET RANDOM SEED
	Random randomGenerator = new Random(); // here, the seed is the current millisecond

        //	FOR CT = 1, WHILE CT <= size, DO
	int size = cards.size();
	for (int ct = 0; ct < size; ct++) {

		//X = RANDOM NUMBER BETWEEN CT AND size INCLUSIVE
		int x = (randomGenerator.nextInt(size - ct)) + ct; // number between ct and size-1

		//	SWAP DECK[CT] WITH DECK[X]
		if (x != ct) {
			Card tempCard = cards.get(ct);
			cards.set(ct, cards.get(x));
			cards.set(x, tempCard);
		}
	}

So, if you have any educated comments or suggestions on how this simple algorithm can be improved or if you see shortcomings, just let us know by posting here or contacting us via support@schnopsn.com

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s