kbjr/node-cards

Random Shuffle not so random.

peazz opened this issue · 5 comments

I seem to be having a random (or not so random) issue with the shuffle function.

Before any cards are shuffled using a fisher yates and seed I have to do an initial shuffle which I am using your built in

  deck.shuffleAll()

However, its not producing a random result at all. The 2nd card is ALWAYS Ace of Club. Is there anywya around this to get a truely random shuffle?

I am reinstalling the package in case I changed something.

One note, in your npm package there is typo, around line 73 you missed out a : so when u try to run the package it breaks.

EDIT: Reinstalled the package and now the 2nd card is always 2 of clubs... Any idea whats going on here?

I just made a test and the first card in the deck never gets swapped, I have my app on a private gibhub repo, I can add you as a collaborator and u can test if you like

kbjr commented

The first thing I would say is that by default, this module uses Math.random for its randomization which is about the crappiest random function known to man. You can tell the module to use the arc4 random number generator instead if you prefer, which may help you:

var cards = require('node-cards');

// Will now use better PRNG for randomization
cards.useArc4 = true;

Second, the actual randomizing/shuffling code can be found in the rand-utils module. If you see something wrong with the algorithm let me know, but I'm pretty sure the fisher-yates is implemented correctly, so its probably just a seeding issue (Math.random).

Your a late night owl eh?

Ok, when I enable the prng it says I need to seed it, but anything I try does not work.

I'm still a beginner with no and serious javascript, throwing myself in at the depend so apologise in advance.

kbjr commented

The seeding should work something like this:

var rand = require('node-cards/node_modules/rand-utils');

rand.arc4.seed({ /* ... any kind of object/value for seeding ... */ });

I could probably make that easier/more clear, but for now that should do it.

kbjr commented

Seeding the ARC4 PRNG can now be done with the cards.seedArc4 method:

var cards = require('node-cards');

// Tell node-cards to use the ARC4 generator
cards.useArc4 = true;

// Seed the ARC4 generator so it can make randomness ..
cards.seedArc4({ /* some kind of seed data */ });

// This should now give a nicely shuffled deck
var deck = new cards.PokerDeck();
deck.shuffleAll();