Current location: Hot Scripts Forums » Programming Languages » PHP » excluding a number from a random


excluding a number from a random

Reply
  #1 (permalink)  
Old 07-13-06, 06:26 PM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
excluding a number from a random

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?
Reply With Quote
  #2 (permalink)  
Old 07-13-06, 06:45 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
thats what id do lol
Reply With Quote
  #3 (permalink)  
Old 07-13-06, 06:50 PM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
what would you do...

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?
Reply With Quote
  #4 (permalink)  
Old 07-13-06, 06:51 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
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???
Reply With Quote
  #5 (permalink)  
Old 07-13-06, 06:58 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
This should work:

PHP Code:

<?php


$card 
= array( /* ... your cards array here ...*/ );

$arr range051 );

$rand1 array_rand$arr ); // first card
unset( $arr[$rand1] ); // first card is removed from deck
$rand2 array_rand$arr ); // second card

$card1 $card[$rand1];
$card2 $card[$rand2];

?>
__________________
The toxic ZCE
Reply With Quote
  #6 (permalink)  
Old 07-13-06, 06:58 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
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???
Reply With Quote
  #7 (permalink)  
Old 07-13-06, 07:14 PM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
awsome thanks all

excellent script mab... thanks.. im addin it right now ^_^
Reply With Quote
  #8 (permalink)  
Old 07-14-06, 07:01 PM
twoeyes twoeyes is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
If that doesnt work out, heres a way to exclude a number

PHP Code:

$exclude 5;

$number $exclude;
while(
$number == $exclude)
{
   
$number rand(min,max);

__________________
there are 10 types of people in the world, those who know binary and those who dont
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Number generator zitwep PHP 7 06-12-06 04:32 AM
random number help stormshadow PHP 4 03-27-06 01:09 PM
Random number guessing nitefire PHP 15 07-28-04 11:36 PM
looking for a random number script ncben Script Requests 3 08-07-03 09:10 AM


All times are GMT -5. The time now is 07:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.