How to use PHP to generate n numbers and set them as values

Source: Internet
Author: User
How can I use PHP to generate n numbers and set them to a specific value? How can I generate n numbers and set them as values? The function is as follows: for example, there are 10 sites randomly allocated to each percentage and the sum is 100%, because you want to select the best allocation scheme from several methods, it will certainly involve loops, so I am a little confused. I hope you can give me some advice. thank you first ., Baidu: how can I use PHP to generate n numbers and set them to a specific value?
How can I generate n numbers and set them as values?

The following functions are available:
For example, there are 10 sites randomly allocated to each percentage and the sum is 100%, because it will certainly involve loops to select the best allocation scheme from several methods, so I'm a little confused. I hope you can give me some advice. thank you first.

------ Solution --------------------
Baidu: integer division.
------ Solution --------------------
Between 1 and, n-1 random numbers are generated, and n + 1 numbers are sorted. The difference between adjacent numbers is the percentage.
------ Solution --------------------
Php code

/**
+ ---------------------------------
* Function: randomly generate 10 percentages, which is 1
* @ Auathor beatyou000081202
* @ Time 2012-6-15
+ ---------------------------------
*/

Print_r (percentage_num ());

Function percentage_num ()
{
// Random percentage storage location
$ Arr_num = array ();

// Initialize the range of the first value
$ Num = 100;

For ($ I = 1; $ I <10; $ I ++)
{
$ Arr_num [$ I] = random_num ($ num );
$ Num = $ num-$ arr_num [$ I];
}

// The Last percentage. subtract the first five and the first five from 100.
For ($ I = 1; $ I <10; $ I ++)
{
$ Add_num + = $ arr_num [$ I];
}

$ Arr_num [10] = 100-$ add_num;

Return $ arr_num;
}

Function random_num ($ num)
{
Return rand (1, $ num/2.5 );
// Here, the random range is reduced to make the result more satisfactory.
}


Hope to be useful to lz haha
------ Solution --------------------
This is not a simple php random generation of numbers. this involves recursive search algorithms.

I have learned a little about algorithms from the C language and data structure and algorithm Forum.

First, the author did not explain to us what is your optimal solution.

However, the author's best practice is certainly in the state space below me. we can think of the one hundred numbers

One one hundred recognize queues, so the first person and 100th people will certainly have 99 gaps,

In fact, we put the nine partitions randomly in the 99 gaps, and we will get the result you want.

So how many methods are there to select 9 for 99 null? The number C () is very large and can be calculated.

However, it may take a lot of time to print all the results.
------ Solution --------------------

He wants to make step 0.01 .....


Discussion

This is not a simple php random generation of numbers. this involves recursive search algorithms.

I have learned a little about algorithms from the C language and data structure and algorithm Forum.

First, the author did not explain to us what is your optimal solution.

However, the author's best practice is certainly in the state space below me. we can think of the one hundred numbers

One one hundred recognize queues, so the first person and 100th people will certainly have 99 gaps,

In fact, we want to place the nine partitions randomly in this 99 ......

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.