[PHP] Randomizing 3 different numbers.
I am tring to output 3 different random numbers between 1 and 20 into 3 different strings. Using Rand() for each string isn't sufficient as I may get the same number repeated for one of the other strings- eg 1,1,3 or 5,3,3 . It's important that I get three different numbers outputted into for example $one, $two, $three. Can anyone help? Ian = - Ian A. Gray Manchester, UK Telephone: +44 (0) 161 224 1635 - Fax: +44 (0) 870 135 0061 - Mobile: +44 (0) 7900 996 328 Business Enquiries: +44(0)870 770 8832 E-mail: [EMAIL PROTECTED]: www.baritone.uk.com (Performance) www.vocalstudio.co.uk (Vocal Tuition)www.selectperformers.com (Web design for professional musicians) - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Randomizing 3 different numbers.
Before you add a number to a string make sure that the number is not in any of the other ones if they exist. Use a do-while loop on it... $one = rand( 1,20 ); do { $two = rand( 1, 20 ); }while( $two == $one ); do { $three = rand( 1, 20 ); }while( ($three == $one) ($three == $two) ); echo One: $one\n; echo Two: $two\n; echo Three: $three\n; HTH -- Ray On Mon, 2003-10-27 at 05:26, Ian Gray wrote: I am tring to output 3 different random numbers between 1 and 20 into 3 different strings. Using Rand() for each string isn't sufficient as I may get the same number repeated for one of the other strings- eg 1,1,3 or 5,3,3 . It's important that I get three different numbers outputted into for example $one, $two, $three. Can anyone help? Ian = - Ian A. Gray Manchester, UK Telephone: +44 (0) 161 224 1635 - Fax: +44 (0) 870 135 0061 - Mobile: +44 (0) 7900 996 328 Business Enquiries: +44(0)870 770 8832 E-mail: [EMAIL PROTECTED]: www.baritone.uk.com (Performance) www.vocalstudio.co.uk (Vocal Tuition)www.selectperformers.com (Web design for professional musicians) - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Randomizing 3 different numbers.
Sorry for top-posting, gotta configure my mail client better later. Small error here: }while( ($three == $one) ($three == $two) ); Change to: }while( ($three == $one) || ($three == $two) ); -Original Message- From: Ray Hunter [mailto:[EMAIL PROTECTED] Sent: den 27 oktober 2003 15:56 To: [EMAIL PROTECTED] Subject: Re: [PHP] Randomizing 3 different numbers. Before you add a number to a string make sure that the number is not in any of the other ones if they exist. Use a do-while loop on it... $one = rand( 1,20 ); do { $two = rand( 1, 20 ); }while( $two == $one ); do { $three = rand( 1, 20 ); }while( ($three == $one) ($three == $two) ); echo One: $one\n; echo Two: $two\n; echo Three: $three\n; HTH -- Ray On Mon, 2003-10-27 at 05:26, Ian Gray wrote: I am tring to output 3 different random numbers between 1 and 20 into 3 different strings. Using Rand() for each string isn't sufficient as I may get the same number repeated for one of the other strings- eg 1,1,3 or 5,3,3 . It's important that I get three different numbers outputted into for example $one, $two, $three. Can anyone help? Ian = - Ian A. Gray Manchester, UK Telephone: +44 (0) 161 224 1635 - Fax: +44 (0) 870 135 0061 - Mobile: +44 (0) 7900 996 328 Business Enquiries: +44(0)870 770 8832 E-mail: [EMAIL PROTECTED]: www.baritone.uk.com (Performance) www.vocalstudio.co.uk (Vocal Tuition)www.selectperformers.com (Web design for professional musicians) - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Randomizing 3 different numbers.
Dont you mean || at the last loop? You loop as long as 3 is equal to both 1 and 2, || would cause it to loop while it's equal to any of them.. -- // DvDmanDT MSN: dvdmandt¤hotmail.com Mail: dvdmandt¤telia.com ## Please, if you are using windows, you may be infected by Swen. Please go here to find out more: http://us.mcafee.com/virusInfo/default.asp?id=helpCenterhcName=swen http://securityresponse.symantec.com/avcenter/venc/data/[EMAIL PROTECTED] ## Ray Hunter [EMAIL PROTECTED] skrev i meddelandet news:[EMAIL PROTECTED] Before you add a number to a string make sure that the number is not in any of the other ones if they exist. Use a do-while loop on it... $one = rand( 1,20 ); do { $two = rand( 1, 20 ); }while( $two == $one ); do { $three = rand( 1, 20 ); }while( ($three == $one) ($three == $two) ); echo One: $one\n; echo Two: $two\n; echo Three: $three\n; HTH -- Ray On Mon, 2003-10-27 at 05:26, Ian Gray wrote: I am tring to output 3 different random numbers between 1 and 20 into 3 different strings. Using Rand() for each string isn't sufficient as I may get the same number repeated for one of the other strings- eg 1,1,3 or 5,3,3 . It's important that I get three different numbers outputted into for example $one, $two, $three. Can anyone help? Ian = - Ian A. Gray Manchester, UK Telephone: +44 (0) 161 224 1635 - Fax: +44 (0) 870 135 0061 - Mobile: +44 (0) 7900 996 328 Business Enquiries: +44(0)870 770 8832 E-mail: [EMAIL PROTECTED]: www.baritone.uk.com (Performance) www.vocalstudio.co.uk (Vocal Tuition)www.selectperformers.com (Web design for professional musicians) - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Randomizing 3 different numbers.
Just to give you something to chew on, try the code below. ?php $myResultsArray = array(); $numberOfRandNumbersToGet = 3; $randomNumberRange= 20; while(count($myResultsArray) $numberOfRandNumbersToGet) { do { $rand = rand(1, $randomNumberRange); }while(in_array($rand, $myResultsArray)); $myResultsArray[] = $rand; } print_r($myResultsArray); // If you really want $one, $two, $three for exactly three results: list($one, $two, $three) = $myResultsArray; echo 1: $one\n2: $two\n3: $three\n; ? It is slightly more generic, and you can change things a little without having to modify a whole bunch of if() statements. -Original Message- From: Ian Gray [mailto:[EMAIL PROTECTED] Sent: 27 October 2003 12:26 To: [EMAIL PROTECTED] Subject: [PHP] Randomizing 3 different numbers. I am tring to output 3 different random numbers between 1 and 20 into 3 different strings. Using Rand() for each string isn't sufficient as I may get the same number repeated for one of the other strings- eg 1,1,3 or 5,3,3 . It's important that I get three different numbers outputted into for example $one, $two, $three. Can anyone help? Ian = - Ian A. Gray Manchester, UK Telephone: +44 (0) 161 224 1635 - Fax: +44 (0) 870 135 0061 - Mobile: +44 (0) 7900 996 328 Business Enquiries: +44(0)870 770 8832 E-mail: [EMAIL PROTECTED]: www.baritone.uk.com (Performance) www.vocalstudio.co.uk (Vocal Tuition)www.selectperformers.com (Web design for professional musicians) - -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php