Returns the sum of three numbers in a range.

Source: Internet
Author: User
The sum of the three numbers in a range, and the value range of the three numbers is 1-6, which must be an integer.

In the first case, the sum of three numbers ranges from 4 to 10, and the three numbers are randomly generated (three numbers can be completely repeated or not all can be repeated)

In the second case, the sum of the three numbers ranges from 11 to 17 and the three numbers are randomly generated (three numbers can be completely repeated or not all can be repeated)


Example:
If all duplicates are allowed, if the sum of the three counts is 15, the three counts may be 5, 5, and 5. if not,
Three numbers can only be a combination of 4, 5, 6, 6, 6, 3, 3, 6, and 6.

Evaluate a function,

// Based on the given sum, and whether three numbers are allowed to be exactly the same as three arrays,
// It is required that all possible combinations, such as 6, 6, 3, 3, 6, and 6, must be returned.
// The program only requires random return of one of all possible combinations.
Function get3nums ($ in several cases, $ whether to allow all duplicates)
// For example, list ($ rand1, $ rand2, $ rand3) = get3nums (2, false); // in the second case, the three numbers cannot all be the same
// Get $ rand1 = 6, $ rand2 = 6, $ rand3 = 3

Function get3nums ($ qingkuang, $ ifxiangtong ){

}


Reply to discussion (solution)

First case
Because there are three numbers, you can first subtract 2 from the input value, then random The first number, and then calculate the remainder.
For example
Function f ($ n)
{
$ T = $ n-2;
$ A = rand (1, min (6, $ t ));
$ T = $ n-$ a-1;
$ B = rand (1, min (6, $ t ));
$ T = $ n-$ a-$ B;
Return array ($ a, $ B, $ c );
}
If you want to avoid three duplicates, add the statement to judge the duplicates.

The preceding variable name is incorrect.
Function f ($ n)
{
$ T = $ n-2;
$ A = rand (1, min (6, $ t ));
$ T = $ n-$ a-1;
$ B = rand (1, min (6, $ t ));
$ C = $ n-$ a-$ B;
Return array ($ a, $ B, $ c );
}

The second case is the same as the first case.
Not all duplicates. you only need to add a judgment before the return value.
If ($ a ==$ B & $ B ==$ c)
{
Return f ($ n );
}

/*** @ $ Lower start range limit * @ $ end range limit * @ $ repeat allows repetition. by default, **/function get3nums ($ start, $ end, $ repeat = false) {do {$ r = array (); $ n = rand ($ start, $ end ); // get a number in the specified range $ a = intval ($ n/3); $ r [] = rand (1, min ($ a, 6 )); $ r [] = rand (1, min ($ a, 6); $ r [] = $ n-array_sum ($ r); if ($ repeat) break ;} while (count (array_count_values ($ r) = 1); return $ r;} print_r (get3nums (4, 10); print_r (get3nums (4, 10, 1); print_r (get3nums (11, 17); print_r (get3nums (11, 17, 1 ));

/*** @ $ Lower start range limit * @ $ end range limit * @ $ repeat allows repetition. by default, **/function get3nums ($ start, $ end, $ repeat = false) {do {$ r = array (); $ n = rand ($ start, $ end ); // get a number in the specified range $ a = intval ($ n/3); $ r [] = rand (1, min ($ a, 6 )); $ r [] = rand (1, min ($ a, 6); $ r [] = $ n-array_sum ($ r); if ($ repeat) break ;} while (count (array_count_values ($ r) = 1); return $ r;} print_r (get3nums (4, 10); print_r (get3nums (4, 10, 1); print_r (get3nums (11, 17); print_r (get3nums (11, 17, 1 ));




No, you have more than 6 numbers between 1 and 6.

Array
(
[0] => 2
[1] => 2
[2] => 4
)
Array
(
[0] => 1
[1] => 1
[2] => 2
)
Array
(
[0] => 4
[1] => 1
[2] => 8
)
Array
(
[0] => 2
[1] => 1
[2] => 8
)

No, you have more than 6 numbers between 1 and 6.


Do you understand?


No, you have more than 6 numbers between 1 and 6.


Do you understand?



The values of the three numbers must be 1-6. The output is 8.

/**
* @ $ Start minimum range
* @ $ End maximum range
* @ $ Repeat: repeat is allowed. it is not allowed by default.
**/
Function get3nums ($ start, $ end, $ repeat = false ){
If (! $ Repeat ){
$ N = rand ($ start, $ end); // gets a number in the specified range
For ($ I = 1; $ I <7; $ I ++ ){
For ($ j = 1; $ j <7; $ j ++ ){
$ K = $ n-$ I-$ j;
If ($ k <7 & $ k> 0 ){
If ($ I = $ j and $ j = $ k )){
Continue;
}
Echo $ I. '-'. $ j. '-'. $ k ."
";
}
}
}
} Else {
$ N = rand ($ start, $ end );
$ Avg = $ n/3;
If ($ avg! = Intval ($ avg )){
Echo "no value "."
";
} Else {
Echo $ avg. '-'. $ avg. '-'. $ avg ."
";
}
}
}

Print_r (get3nums (4, 10 ));
Echo'
';
Print_r (get3nums (4, 10, 1 ));
Echo'
';
Print_r (get3nums (11, 17 ));
Echo'
';
Print_r (get3nums (11, 17, 1 ));
?>

Check again.

Function get3nums ($ start, $ end, $ repeat = false) {do {$ r = array (); $ n = rand ($ start, $ end ); // get a number in the specified range $ a = intval ($ n/3); $ r [] = rand (1, min ($ a, 6 )); $ r [] = rand (1, min ($ a, 6); $ r [] = $ n-array_sum ($ r );} while (max ($ r)> 6); if ($ repeat) break;} while (count (array_count_values ($ r) = 1); return $ r ;}

/**
* @ $ Start minimum range
* @ $ End maximum range
* @ $ Repeat: repeat is allowed. it is not allowed by default.
**/
Function get3nums ($ start, $ end, $ repeat = false ){
If (! $ Repeat ){
$ N = rand ($ start, $ end); // gets a number in the specified range
For ($ I = 1; $ I <7; $ I ++ ){
For ($ j = 1; $ j <7; $ j ++ ){
$ K = $ n-$ I-$ j;
If ($ k <7 & $ k> 0 ){
If ($ I = $ j and $ j = $ k )){
Continue;
}
Echo $ I. '-'. $ j. '-'. $ k ."
";
}
}
}
} Else {
$ N = rand ($ start, $ end );
$ Avg = $ n/3;
If ($ avg! = Intval ($ avg )){
Echo "no value "."
";
} Else {
Echo $ avg. '-'. $ avg. '-'. $ avg ."
";
}
}
}

Print_r (get3nums (4, 10 ));
Echo'
';
Print_r (get3nums (4, 10, 1 ));
Echo'
';
Print_r (get3nums (11, 17 ));
Echo'
';
Print_r (get3nums (11, 17, 1 ));
?>



There are still many errors. In addition, such a tie may appear 6, which is not ideal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.