i want to make a random number that excludes a number... (black jack)
here is what i have so far....
PHP Code:
//set cards as an array
$card = array("Ace of Hearts","Two of Hearts","Three of Hearts","Four of Hearts","Five of Hearts","Six of Hearts","Seven of Hearts","Eight of Hearts","Nine of Hearts","Ten of Hearts","Jack of Hearts","Queen of Hearts","King of Hearts","Ace of Diamonds","Two of Diamonds","Three of Diamonds","Four of Diamonds","Five of Diamonds","Six of Diamonds","Seven of Diamonds","Eight of Diamonds","Nine of Diamonds","Ten of Diamonds","Jack of Diamonds","Queen of Diamonds","King of Diamonds","Ace of Clubs","Two of Clubs","Three of Clubs","Four of Clubs","Five of Clubs","Six of Clubs","Seven of Clubs","Eight of Clubs","Nine of Clubs","Ten of Clubs","Jack of Clubs","Queen of Clubs","King of Clubs","Ace of Spades","Two of Spades","Three of Spades","Four of Spades","Five of Spades","Six of Spades","Seven of Spades","Eight of Spades","Nine of Spades","Ten of Spades","Jack of Spades","Queen of Spades","King of Spades");
//Set dealer cards..
$rand1 = rand(0,51);
$rand2 = rand(0,51);
$card1 = "$card[$rand1]";
$card2 = "$card[$rand2]";
but now i want to make rand2 exclude the rand1 number... is that possible? or should i just make it so that if it equals it than try again?
if it equals than try again? if so than thats the hard way because when you get hits... then it will get complicated... :-/ so i wanna keep it simple and just try to exclude.. but is it possible?
Using two random numbers, you would need to do what you say, get a new second number if it is equal to the first. However, in thinking about this, here is a way to get two random cards -
PHP Code:
$card = array(...); // your original array...
shuffle($card); // randomize the array
$rand2 = rand(1,51); // get a random number 1-51 (exclude 0)
$card1 = $card[0]; // get the zero element (which is random)
$card2 = $card[$rand2]; // get some other card using random index
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
If you are doing a card game, where cards become unavailable once they are delt out, I recommend the following -
PHP Code:
$card = array(...); // your original array...
shuffle($card); // randomize the array
$card1 = array_pop($card); // get and remove the last element in the array
$card2 = array_pop($card); // get the next card
...
$next_card = array_pop($card); // get the next card
.
.
.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???