Generate random 7-carded poker hand for a given hand type

4

1

Represent poker cards as numbers from 0 to 51. 0 to 12 - all hearts from 2 to Ace, 13 to 25 all diamonds and so on. Your goal is to write a function for each poker hand type which generates a random 7-card hand (as in Texas Holdem on the river), in which the best 5-card combination is of the target type. The output is a sequence of 7 numbers. So, your function accepts a number from 0 to 8, where 0 is a High Card poker hand type and 8 is a Straight Flush, and returns a sequence of 7 numbers which represent the hand.

Additional requirements:

  1. The hand should be sampled uniformly from all the possible hands of the given type. For example, it's not ok to generate straight flushes which always end with Ace.

  2. You can't use or generate any look up tables bigger then 1000 values. For example, it's not ok to generate all flushes and than select a random one from them.

  3. Generating completely random sets of 7 cards and checking for the target type until it is found is not allowed. Basically your function should have defined upper time limit.

GrayR

Posted 2014-06-22T09:28:23.470

Reputation: 149

Question was closed 2016-10-14T21:52:55.687

I voted to close. Questions should be self-contained. This means any necessary information should not simply be "linked" to. So, add a list of all poker hand types and how they're defined, and specify the suits and their order for those who don't know (you just said "and so on"). Also, I think requirement 2 could be dropped if you simply require the program to complete in decent time. – mbomb007 – 2016-10-14T18:52:36.423

What's the (objective) winning criterion? – Martin Ender – 2014-06-22T09:31:06.533

@m.buettner added tag for popularity contest. Basically I solved this problem straightforwardly on scala myself, but code is too big and ugly to be proud of and I can't find nice and clear algorithm, which I believe exists. – GrayR – 2014-06-22T09:38:57.163

6

If you're looking for a nice and clear algorithm codegolf.SE is not the right place to ask for it, regardless of the winning criterion. You might want to try codereview.SE instead. As for your challenge, I think it could be interesting as a plain code golf challenge (shortest code wins).

– Martin Ender – 2014-06-22T09:44:55.867

@m.buettner I already have working algorithm, and the only reason why I created this question is because I believe the task is tricky and can be fun for others. – GrayR – 2014-06-22T09:47:50.370

4I'm not sure why already having a working algorithm prevents you from posting it on codereview.SE. In fact, that's exactly what CR is for - you've got a solution, but you don't think it's optimal, so you want others to have a look at it and see if they can improve it. Anyway, retracting my close vote. As code golf it sounds like good fun. – Martin Ender – 2014-06-22T09:51:26.413

@m.buettner maybe I'll do that, thanks for suggestion. – GrayR – 2014-06-22T09:52:26.903

2The new rule 3 forbids the most obvius solution. Now, what about generating a random set then 'count up' until the target type is found? – edc65 – 2014-06-22T13:35:53.390

@edc65 I think that is acceptable, you only need to make sure that your 'count up' method satisfies rule number 1 and allows generation of all possible hands. – GrayR – 2014-06-22T13:59:30.690

Does constant seed count as "defined upper limit"? :) – seequ – 2014-06-22T17:16:52.783

No answers