Jump to content

Exclude a number in rand function?


Monkuar

Recommended Posts

Long story short, this is part of myblack jack game when dealing cards

 

$rand1 = rand(1, 11);
$rand2 = rand(1, 10);

 

 

Max of 21, okay so.... I need a function or something to exclude that number.

 

Let's say user got 5 from $rand2.  Now I need to make the $rand2 have a smaller percentage chance to spit out a 5 again.  (I can use that a global variable, everything is in a database and serverside)

 

Okay now a user get's 5 again from $rand2, I need to decrease the likelyhood of drawing  a 5 again.

 

Okay now the user hit's all 5  4 CARDS, now I need to make $rand2 variable exclude the number 5 because the guy already had 4 cards of 5,  possible?

Link to comment
Share on other sites

What you need to do is keep track of the deck.

 

Protip: make your code model your... well, model.

$cards = range(1, 52);
shuffle($cards);

$first_player = $house = array();
$first_player[] = array_shift($cards);
$house[] = array_shift($cards);
$first_player[] = array_shift($cards);
$house[] = array_shift($cards);

1-13 is one suit, 14-26 is another (subtract 13), 27-39 is another (subtract 26), and 40-52 is the last (subtract 39).

Link to comment
Share on other sites

If you are dealing a shuffled deck, there is NO less chance of getting a 5 after a 5 than after any other card. The chance of getting 4 5s is the same as the chance of getting 5, 6, 7, 8 or 5, K, 3, 7.

What you should do is create a deck array that has all 52 cards in it.

 

$deck = array();
$suits = 4;
$cards = 13;

for($i=1; $i<=$suits; $i++){
   for($c=1; $c<=$cards; $c++){
    $deck[] = $c.$i; // (you could make $suits and $cards arrays with the names and use a foreach instead)
   }
}

now use $deck and the array functions to shuffle it, then pull cards off it.

 

 

EDIT: same as above, didn't see your reply sorry.

Link to comment
Share on other sites

If you are dealing a shuffled deck, there is NO less chance of getting a 5 after a 5 than after any other card.

Uh yes, it is less likely. After pulling a non-5 there is a 4/51 chance of getting one, but after pulling a 5 there is a 3/51 chance.

 

 

The chance of getting 4 5s is the same as the chance of getting 5, 6, 7, 8 or 5, K, 3, 7.

No?

5, 5, 5, 5: 4/52 * 3/51 * 2/50 * 1/49 = 0.0000369%

all unique: 4/52 * 4/51 * 4/50 * 4/49 = 0.00394%

 

 

What you say is true only if you pull a card, put it back, shuffle, and pull again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.