Re: [PHP] odd and even numbers
On Fri, 2004-01-16 at 13:17, Jake McHenry wrote: I have a random number being generated from 1 to 501, is there an easy way for me to tell if the result is an odd or even number? Yep. The modulus operator (%). Your number mod 2 will be 1 if it is odd and 0 if it is even. http://us4.php.net/manual/en/language.operators.arithmetic.php if ($your_number % 2) { echo 'odd'; } else { echo 'even'; } - Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] odd and even numbers
function isOdd ($value) { return (int)$value % 2 ? true : false; } Returns true/false if the value is odd. Also rounds down floating point numbers given as the test value. -Jeremy -Original Message- From: Jake McHenry [mailto:[EMAIL PROTECTED] Sent: Friday, January 16, 2004 2:18 PM To: [EMAIL PROTECTED] Subject: [PHP] odd and even numbers I have a random number being generated from 1 to 501, is there an easy way for me to tell if the result is an odd or even number? Thanks, Jake -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] odd and even numbers
On Fri, Jan 16, 2004 at 02:30:59PM -0600, Jeremy wrote: function isOdd ($value) { return (int)$value % 2 ? true : false; } Returns true/false if the value is odd. Also rounds down floating point numbers given as the test value. No need to explicitly state true and false. Just negate the result of your modulus operator, and take advantage of the fact that 0 is false, and 1 is true: function isOdd ($value) { return !((int)$value % 2); } -- [ joel boonstra | gospelcom.net ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] odd and even numbers SOLVED
- Original Message - From: joel boonstra [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, January 16, 2004 3:35 PM Subject: Re: [PHP] odd and even numbers On Fri, Jan 16, 2004 at 02:30:59PM -0600, Jeremy wrote: function isOdd ($value) { return (int)$value % 2 ? true : false; } Returns true/false if the value is odd. Also rounds down floating point numbers given as the test value. No need to explicitly state true and false. Just negate the result of your modulus operator, and take advantage of the fact that 0 is false, and 1 is true: function isOdd ($value) { return !((int)$value % 2); } -- [ joel boonstra | gospelcom.net ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Thanks everyone for the fast replys! Jake -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] odd and even numbers
On Fri, 2004-01-16 at 15:17, Jake McHenry wrote: I have a random number being generated from 1 to 501, is there an easy way for me to tell if the result is an odd or even number? (1 number) ? odd : even; E.g. ?php $i=1; echo 'pre'; while($i 10) { (1 $i) ? print($i . ' is odd') : print($i . ' is even'); echo \n; $i++; } echo '/pre'; ? -- BrianGnuPG - KeyID: 0x04A4F0DC | URL: www.gfx-design.com/keys Key Server: pgp.mit.edu == gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC GnuPG: http://gnupg.org http://www.biglumber.com/x/web?qs=0x2C35011004A4F0DC Linux Registered User #339825 at http://counter.li.org signature.asc Description: This is a digitally signed message part
Re: [PHP] odd and even numbers
- Original Message - From: joel boonstra [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, January 16, 2004 3:35 PM Subject: Re: [PHP] odd and even numbers On Fri, Jan 16, 2004 at 02:30:59PM -0600, Jeremy wrote: function isOdd ($value) { return (int)$value % 2 ? true : false; } Returns true/false if the value is odd. Also rounds down floating point numbers given as the test value. No need to explicitly state true and false. Just negate the result of your modulus operator, and take advantage of the fact that 0 is false, and 1 is true: function isOdd ($value) { return !((int)$value % 2); } -- [ joel boonstra | gospelcom.net ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php This worked.. somewhat.. here is what I want to do though. I have two people checking the same email address. I want to break up the emails 50/50 to each person. What values can I put into the random number generator so that I can get closer to 50/50. I just set up a test page with: $rand = rand(1, 501); echo $rand; if ($rand % 2 == 0) echo even; else echo odd; and I'm getting a lot more evens than odds, so the one person would get more emails than the other. Is there any substitution to rand that I could use to get one message to go to one person, then the next to the other? I'd like it to be as close to 50/50 as possible. any questions, let me know Thanks, Jake -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] odd and even numbers
Jake, This worked.. somewhat.. here is what I want to do though. I have two people checking the same email address. I want to break up the emails 50/50 to each person. What values can I put into the random number generator so that I can get closer to 50/50. I just set up a test page with: $rand = rand(1, 501); echo $rand; if ($rand % 2 == 0) echo even; else echo odd; and I'm getting a lot more evens than odds, so the one person would get more emails than the other. Is there any substitution to rand that I could use to get one message to go to one person, then the next to the other? I'd like it to be as close to 50/50 as possible. Is there any way you can save state? That is, can you keep track of who got the last one, and use that to determine who should get the next one? Otherwise, why not just rand() between 1 and 2? rand(1,2) will, on average, give you as many 1s as 2s (like flipping a coin). There will likely be runs of 1 or 2, but over time, doing rand() with two possible values versus rand() with 501 values will not be significantly different. In fact, since there is one more odd than even between 1 and 501 (inclusive), you will be getting slightly more odds than evens with that range than you will with a range of 1 and 2. Also, which PHP version are you using? If before 4.2.0, you should seed the random number generator with srand(). If you are using 4.2.0 or greater, and still seeing non-random behavior, I don't really have an answer. -- [ joel boonstra | gospelcom.net ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] odd and even numbers
- Original Message - From: Katie Dewees [EMAIL PROTECTED] To: Jake McHenry [EMAIL PROTECTED] Sent: Friday, January 16, 2004 4:44 PM Subject: RE: [PHP] odd and even numbers Can you just set a value somewhere (in a flat file, or a database) to 1 or 2. 1 can represent person number 1, 2 person number 2. Check the value every time you deliver an e-mail to see who gets it, and then switch it to the other one. Katie Dewees Web Developer E-mail: [EMAIL PROTECTED] -Original Message- From: Jake McHenry [mailto:[EMAIL PROTECTED] Sent: Friday, January 16, 2004 3:42 PM To: joel boonstra Cc: [EMAIL PROTECTED] Subject: Re: [PHP] odd and even numbers - Original Message - From: joel boonstra [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, January 16, 2004 3:35 PM Subject: Re: [PHP] odd and even numbers On Fri, Jan 16, 2004 at 02:30:59PM -0600, Jeremy wrote: function isOdd ($value) { return (int)$value % 2 ? true : false; } Returns true/false if the value is odd. Also rounds down floating point numbers given as the test value. No need to explicitly state true and false. Just negate the result of your modulus operator, and take advantage of the fact that 0 is false, and 1 is true: function isOdd ($value) { return !((int)$value % 2); } -- [ joel boonstra | gospelcom.net ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php This worked.. somewhat.. here is what I want to do though. I have two people checking the same email address. I want to break up the emails 50/50 to each person. What values can I put into the random number generator so that I can get closer to 50/50. I just set up a test page with: $rand = rand(1, 501); echo $rand; if ($rand % 2 == 0) echo even; else echo odd; and I'm getting a lot more evens than odds, so the one person would get more emails than the other. Is there any substitution to rand that I could use to get one message to go to one person, then the next to the other? I'd like it to be as close to 50/50 as possible. any questions, let me know Thanks, Jake -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php I could, but I didn't really want to. I just wanted a quick fix... I was going to do that before, just wanted to see if anyone knew of another route. Thanks, Jake -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] odd and even numbers
- Original Message - From: joel boonstra [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, January 16, 2004 4:54 PM Subject: Re: [PHP] odd and even numbers Jake, This worked.. somewhat.. here is what I want to do though. I have two people checking the same email address. I want to break up the emails 50/50 to each person. What values can I put into the random number generator so that I can get closer to 50/50. I just set up a test page with: $rand = rand(1, 501); echo $rand; if ($rand % 2 == 0) echo even; else echo odd; and I'm getting a lot more evens than odds, so the one person would get more emails than the other. Is there any substitution to rand that I could use to get one message to go to one person, then the next to the other? I'd like it to be as close to 50/50 as possible. Is there any way you can save state? That is, can you keep track of who got the last one, and use that to determine who should get the next one? Otherwise, why not just rand() between 1 and 2? rand(1,2) will, on average, give you as many 1s as 2s (like flipping a coin). There will likely be runs of 1 or 2, but over time, doing rand() with two possible values versus rand() with 501 values will not be significantly different. In fact, since there is one more odd than even between 1 and 501 (inclusive), you will be getting slightly more odds than evens with that range than you will with a range of 1 and 2. Also, which PHP version are you using? If before 4.2.0, you should seed the random number generator with srand(). If you are using 4.2.0 or greater, and still seeing non-random behavior, I don't really have an answer. -- [ joel boonstra | gospelcom.net ] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php version 4.2.2 Jake -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php