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
https://forums.phpfreaks.com/topic/264035-exclude-a-number-in-rand-function/
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).

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.