473,797 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to get more precise number (Poker Hands Generator)

7 New Member
Hello Everyone,

I did a poker program in Java that essencially finds the strenght of a poker hand created Randomly. My program is doing OK...but I'm pretty sure it can be optimised.

This is my results with 1 million hands:



Number of hands generated : 1000000
Straight Flush : 0.0012 % In theory : 0.0012%
Four Of A Kind : 0.017 % In theory : 0.0240%
Full House : 0.1535 % In theory : 0.1441%
Flush : 0.2285 % In theory : 0.1967%
Straight : 0.3104 % In theory : 0.3532%
Three Of A Kind : 2.2372 % In theory : 2.1128%
Deux Pairs : 4.6104 % In theory : 4.759%
Pair : 41.777 % In theory : 42.2569%
Nothing : 50.6648 % In theory : 50.157%

There is big differences between my results and the theory. I thought at first that it was because I didn't used enough hands.

This is my results with 50 million hands:



Number of hands generated : 50000000
Straight Flush : 0.0012 % In theory : 0.0012%
Four Of A Kind : 0.0183 % In theory : 0.0240%
Full House : 0.1489 % In theory : 0.1441%
Flush : 0.2286 % In theory : 0.1967%
Straight : 0.3086 % In theory : 0.3532%
Three Of A Kind : 2.2266 % In theory : 2.1128%
Deux Pairs : 4.6107 % In theory : 4.759%
Pair : 41.7652 % In theory : 42.2569%
Nothing : 50.6918 % In theory : 50.1570%

And again, the numbers are pretty much the same so it means that thereare flaws in my logic. The problem is I can't find those flaws. My question will be about my isFourOfAKind() method because I get approximalety 20% less Four Of A Kind hands then the theory. A pretty good difference in my opinion.

In general, my program is built like this

Card --> suit, strenght.
Hand --> Card1, Card2, Card3, Card4, Card5.

This is a representation of my method (Card1 = the strenght of Card1. In reality Card1 would be replace by Card1.getCardSt renght())

if(Card1 == Card2 && Card1 == Card3 && Card1 == Card4 && Card1 != Card5)
return true
else if(Card1 == Card2 && Card1 == Card3 && Card1 != Card4 && Card1 == Card5)
return true
if(Card1 == Card2 && Card1 != Card3 && Card1 == Card4 && Card1 == Card5)
return true
if(Card1 != Card2 && Card1 == Card3 && Card1 == Card4 && Card1 == Card5)
return true
else
return false

I'm pretty sure this logic is good. The problem is my results tells me that my logic is NOT good. Where is the flaw in that method?

Thanks in advance and pardon me for English mistakes...it isn't my first language

kinghippo423

P.S. I saw lots of poker hands generator like mice that works WAY faster than mine. Is there any website that explain some technics to have a "faster" program? It sucks to wait 1 minute ot generate 1 million hands...I hope I explain that well.
Sep 4 '07 #1
13 2558
Ganon11
3,652 Recognized Expert Specialist
If you have translated your results correctly, then I see a 0.6% difference between your results and theory. It looks like your program is doing an adequate job of randomly generating hands - remember, nothing matches theory in practice.
Sep 4 '07 #2
kinghippo423
7 New Member
Theory almost never matched in practice.

By doing a test with 50 millions hands, I should have a really accurate result...specia ly with isFourOfAKind() because I have only 5 cases (XOOOO, OXOOO, OOXOO, OOOXO, OOOOX).
Sep 4 '07 #3
JosAH
11,448 Recognized Expert MVP
Card --> suit, strenght.
Hand --> Card1, Card2, Card3, Card4, Card5.

This is a representation of my method (Card1 = the strenght of Card1. In reality Card1 would be replace by Card1.getCardSt renght())

Expand|Select|Wrap|Line Numbers
  1. if(Card1 == Card2 && Card1 == Card3 && Card1 == Card4 && Card1 != Card5)
  2. return true
  3. else if(Card1 == Card2 && Card1 == Card3 && Card1 != Card4 && Card1 == Card5)
  4. return true
  5. if(Card1 == Card2 && Card1 != Card3 && Card1 == Card4 && Card1 == Card5)
  6. return true
  7. if(Card1 != Card2 && Card1 == Card3 && Card1 == Card4 && Card1 == Card5)
  8. return true
  9. else
  10. return false
  11.  
What if a four of a kind are to be found in card#2, card#3, card#4 and card#5?
I'm about sure that the above logic is incomplete.

kind regards,

Jos
Sep 4 '07 #4
Nepomuk
3,112 Recognized Expert Specialist
What if a four of a kind are to be found in card#2, card#3, card#4 and card#5?
I'm about sure that the above logic is incomplete.
True, you said yourself that there are 5 possibilities, but you only check 4! Try to figure out, which one is missing. ^^

Greetings,
Nepomuk
Sep 4 '07 #5
JosAH
11,448 Recognized Expert MVP
True, you said yourself that there are 5 possibilities, but you only check 4! Try to figure out, which one is missing. ^^

Greetings,
Nepomuk
Yep, and it statistically answers the question that the four of a kind score is
about 20% too low. Problem solved ;-)

kind regards,

Jos
Sep 4 '07 #6
kinghippo423
7 New Member
I was probably smooking that day. I correct that really dumb answer and this is my results after 50 millions hands. The results are as bad but reversely:

Nombre de mains généré : 50000000
Straight Flush : 0.0011 % In theory : 0.0012%
Four Of A Kind : 0.0354 % In theory : 0.0240%
Full House : 0.1419 % In theory : 0.1441%
Flush : 0.2264 % In theory : 0.1967%
Straight : 0.3069 % In theory : 0.3532%
Three Of A Kind : 2.1676 % In theory : 2.1128%
Deux Pairs : 4.5608 % In theory : 4.759%
Pair : 41.5571 % In theory : 42.2569%
C'est rien : 51.003 % In theory : 50.1570%

As you can see, now after 50 millions hands the difference between results and theory are at 0.0114%. That's almost 50% more F-O-A-K than theory. Now I know that my logic is correct thanks to all of you. But still, I'm still with that problem.

This is my new question. Throughtout my program, I check the strenght of the hand by checking all the possibilities. For example, for a pair there is 10 possibilities : XXOOO, XOXOO, XOOXO, XOOOX, OXXOO, OXOXO, OXOOX, OOXXO, OOXOX, OOOXX. I double checked my isPair() method and the logic is perfect. But still, I'm almost a 1% off the theory and I'm using 50 millions hands and that'a a lot.

Is it the optimal way to check the strenght of a poker hand. Is there anyone here that used a different approch? I'm not trying to get a easy answer from all of you (because I will probably re-do my progran with piles and other concepts and I learned in the last year) but you open my mind because in my mind, that's the only way to check the strenght of a poker hand...is by checking every possibilities. My method for my isTwoPairs() method is huge because there is 30 way to manipulate XXOOJ (J in that case is a non-full-house card) without duplication.

My method is excellent? Great? Good 'nouf? Please come again?

BTW, the random Java Class is trustable? I'm starting to suspect that something isn't right with that class.

Oh and By the Way again, what is exactly the seed in the Random Class? I randomize the seed for better result but is it a good way to do it? Should I keep the seed fixed?

Thanks you all agai for the advises.
Sep 5 '07 #7
Ganon11
3,652 Recognized Expert Specialist
Random number generators are, in fact, rarely Random. What they do is reference a very long list of integers that are 'random' - a portion of this list may be:

Expand|Select|Wrap|Line Numbers
  1. ...324845, 348723, 876334, 3984, 4358,...
The seed tells the generator where along the list to begin. If you keep the seed constant, you will be getting the same 'random' number every time, so randomizing the seed is essential.
Sep 5 '07 #8
kinghippo423
7 New Member
The seed tells the generator where along the list to begin. If you keep the seed constant, you will be getting the same 'random' number every time, so randomizing the seed is essential.
But Randomizing the seed doesn't makes the Random numbers really Random though because Math.Random() will never be "Pure" Random. Am I correct?

P.S. Maybe too much Random in that sentence :)
Sep 5 '07 #9
JosAH
11,448 Recognized Expert MVP
My method is excellent? Great? Good 'nouf? Please come again?

Thanks you all agai for the advises.
Mwah, I'd call it so-so. I once did it like this: create two char arrays, one for the
ranks (strengths) and one for the suits:

Expand|Select|Wrap|Line Numbers
  1. char[4] suits= new char[4];
  2. char[15] ranks= new char[15];
  3. Arrays.fill(suits, '0');
  4. Arrays.fill(ranks, '0');
  5.  
Note the fifteen elements in the ranks array; the first two elements are unused
because there are no 0 and 1 cards; aces are high (14) and all cards have a
rank equal to their, ahem, rank.

For five cards in a hand update the two arrays:

Expand|Select|Wrap|Line Numbers
  1. for (Card card: hand) {
  2.    suits[card.getSuit()]++;
  3.    ranks[card.getRank()]++;
  4. }
  5.  
... and convert the arrays to Strings:

Expand|Select|Wrap|Line Numbers
  1. String s= new String(suits);
  2. String r= new String(rank);
  3.  
And here comes the fun: use regular expressions for finding the score:
Four of a kind is found as follows:

Expand|Select|Wrap|Line Numbers
  1. boolean fook= r.matches("0*40*");
  2.  
A full house is found like this:
Expand|Select|Wrap|Line Numbers
  1. boolean fh= r.matches("0*20*30*") || r.matches("0*30*20*");
  2.  
Checking whether or not all cards have the same suit is easy too:

Expand|Select|Wrap|Line Numbers
  1. boolean ss= s.matches("0*40*");
  2.  
I hope you get the picture.

kind regards,

Jos
Sep 5 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3423
by: james blair | last post by:
Hi I am generating a random number using random.randint(1,10000000000) Whats the possibility that the numbers generated will be same when generated by 100 users at the same time? Whats the best method to generate random numbers so that they are most likely unique?? Thanks
70
6288
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not using rand() if you want more than approx. UINT_MAX total different sequences, and so on. So I...
7
3648
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: 3.141593
5
3354
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. Anybody have an easy and fast...
1
4487
by: Martin Olsen | last post by:
Hi all. I am creating a program which calculates poker odds. The program should look at the visible cards (those on your hand and those on the table) then count the cards needed to improve the hand(eg. how many cards do I need to get a Flush) and then calculate the odds based on those numbers. My problem is that I do not know how I should find the missing cards. One way I could do it is to use programming logic like this
27
5631
by: Simon Biber | last post by:
I was reading http://en.wikipedia.org/wiki/Poker_probability which has a good description of how to count the frequency of different types of poker hands using a mathematical approach. A sample Python program is given in the discussion page for doing it that way. I wanted to take a different approach and actually generate all the possible hands, counting the number of each type. It's quite do-able on today's hardware, with 5-card...
4
8915
by: hardieca | last post by:
Has anyone heard of an open-source .NET engine that calculates the winning and losing percentages of hands? Regards, Chris
9
4687
by: teejayem | last post by:
I am looking for some help! I am currently developing a new game for an online community. The game is called Pokino which ishalf bingo and half poker. Wierd eh?! Anyway... The final part of the program needs to evaluate two 5 card poker hands and determine which one out of the two hands wins. I thought rather than create a new class of my own I would see if there was anything
20
5140
by: A | last post by:
Hi all. Is this a bug or what??? here is a simple code: <?php mt_srand(1); echo mt_rand(0, 255)."<br />"; echo mt_rand(0, 255)."<br />";
0
9536
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10468
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9063
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.