Click here to Skip to main content
13,044,398 members (119,945 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I wrote a card game simulation for a very simple card game. The simulation runs the game multiple times and doesn't display output except for the number of wins, losses, and win %. I ran multiple simulations of millions, 10s of millions, and even 100 million games, which took several minutes. After more than 1 million games the win % tends to cluster around 1.62%. However, the mathematical win % should be 1.8316% which was confirmed by a professor of mathematics. So I should be winning 1 of every 500 games more than I am. I've checked my code over and over including what I believe to be a good use of the random number generator and the proper way to shuffle a deck of cards.

I'm wondering if someone would be willing to write their own procedure to simulate this game and see what they get for results?

The game goes like this. Shuffle the deck. Then one by one place each card face up and call out, in order, "ace" "two" "three" ... "jack" "queen" "king". Then repeat until the end of the deck. You win only if each card does not match the called out value. For example, if you get to the 5th card and it is a "5" you lose. If you get to the 28th card and it is a 2 you lose. So, obviously, you test using the mod operator % 13. I don't want to bias another solution with a post of code, but thought someone might be interested to try it themselves and, if closer to the mathematical expectation, compare code.
Posted 30-Nov-12 3:40am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

My thought on your issue is that you are probably running into an issue with the pseudo-random number generator. The problem is that it isn't truly random. For most instances, this isn't a problem but because you are generating such quantities, you might be running into a problem with it.

My suggestion would be to change your generator to increase its randomness and see if that brings you closer to your expected result. Here are some options for you to try:[^]

As for us developing one as well, this isn't the place to ask for something like that. You won't get the exposure to your request that you want and it really doesn't fit as a question.
woj123 30-Nov-12 15:09pm
Thank you for the pseudo random numbers link. Just in case anyone else is following this, based on Tim's link which contains good information on random number generation I replaced my random number generator with the RNGCryptoServiceProvider.GetBytes Method found in that link. Unfortunately, after a 10 million game simulation, I ended up with 1.6229% as a win percentage which follows my original findings.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 30 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100