Control the dice size

Source: Internet
Author: User

When the three dice are rolled out together, their sum is not fixed. They may be big or small. Maybe only those high people can control them at will. if we move this game online, it will be different. We can also control the number of dice to achieve the purpose of winning.

The program I wrote below must have many shortcomings. I just want to give you an example to use it as an example.

<? PHP
// Random value from an array
Function get_rand ($ array)
{
If (! Empty ($ array ))
{
Srand (float) microtime () * 10000000 );
$ K = array_rand ($ array, 1 );
Return $ array [$ K];
}
Return "0 ";
}

// Design a random extraction result with a small frequency
Function get_rank ($ range, $ total, $ max)
{
$ Arr = array ();
$ Arr = array_pad ($ arr, $ range, "0 ");
$ Arr = array_pad ($ arr, $ total, "1 ");
$ M = get_rand ($ ARR );
If ($ M = "0 ")
{
$ P = get_rand (range (3, 10 ));
}
Else
{
$ P = get_rand (range (11, 18 ));
}
Return split_num ($ P, $ max );
}

// Divide a number into two numbers and
Function div_num ($ num, $ max)
{
$ M = rand (Ceil ($ num/2), min ($ Max, $ num-1 ));
Return $ m;
}

// Divide a number into three numbers and
Function split_num ($ num, $ max)
{
$ Arr = array ();
For ($ I = 1; $ I <= $ Max; $ I ++)
{
If ($ num-$ I <= 2 * $ Max & $ num-$ I> = 2)
{
$ Arr [] = $ I;
}
}
$ M = get_rand ($ ARR );
$ M1 = div_num ($ num-$ M, $ max );
$ M2 = $ num-$ M-$ M1;
$ R_arr = array ($ M, $ M1, $ m2 );
Shuffle ($ r_arr );
Return $ r_arr;
}

// Determine the dice result based on the winning rate
Function set_dice_num ($ userselect)
{
Global $ _ config;
List ($ range, $ total) = explode ("/", $ _ config ["winrange"]);
If ($ userselect = "1") // select small
{
Return get_rank ($ range, $ total, 6 );
}
Else if ($ userselect = "2 ")
{
Return get_rank ($ total-$ range, $ total, 6 );
}
}

// The test is as follows:
$ _ Config ["winrange"] = "1/5"; // set the winning rate. The score is expressed as a score. The score 1/5 indicates that only one win is allowed for five chances, set it to 1/100 ~~

$ J = "0"; // counter
For ($ I = 0; $ I <1000; $ I ++) // cast 1000 times
{
$ Sum = array_sum (set_dice_num (1); // 1 indicates that the user casts a small value. set_dice_num (1) is used to generate an array containing three values, is the display Number of Three dice. array_sum is the sum of the computing data elements.
If ($ sum <"11") // if it is small, count
{
$ J ++;
}
}

    echo $j;
?>

 

From the final output result, you will find that $ J is about 200. If the user invests a large value, you can use set_dice_num (2) to generate an array, A large number of times must be around 200.

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.