根据人数随机分配百分比的问题
需求是这样的,根据可变参数 $people 来给每个人随机分配百分比,条件是人数一定会控制在 3
写了个简单的分配,但会出现最后一个人会得到很多的情况,并且由于分配区间实际上是 1 - 平均数 之间的百分比,不是严格意义上的 “随机平均分配”,求各位大大给个思路,抛砖引玉,不甚感谢。
$people = 3; //人数 $percent = 100; //百分比 $average = floor($percent/$people); $rand_array = array(); $count = 0; for ($i=0; $i < $n; $i++) { if($i == ($n - 1)){ $rand_array[$i] = 100 - $count; }else{ $rand_array[$i] = rand(1,$average); $count = $count + $rand_array[$i]; } } return $rand_array;
回复讨论(解决方案)
这样可能好点
$people = 3; //人数$percent = 100; //百分比$res = array_fill(0, 3, floor($percent/$people)); //平均分配$d = 5; //容差foreach($res as &$v) $v += rand(-$d, $d);$res[rand(0, $people-1)] += $percent - array_sum($res); //随机将残差补入print_r($res);
忘记上来结贴了,感谢版主大大的回复,问题已经解决了,贴出来与大家分享:
public static function rand_bouns($person){ //百分比 $percent = 100; $now_person = $person; $bouns = array(); for($i=0;$i<=$person-1;$i++){ $bouns[$i] = self::get_bouns($now_person,$percent); $percent = $percent - $bouns[$i]; $now_person = $now_person - 1; $now_bouns += $bouns[$i]; } return $bouns; }public static function get_bouns($person,$percent){ if($person==1) return $percent; $max = 30; if($percent < $max) $max = $percent; $min = $percent-$max*($person-1) <= 0 ? 1 : $percent-$max*($person-1); $max = $max-($person) <= 0 ? 1 : $max-($person); return rand($min,$max); }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The ability to generate random numbers or alphanumeric strings comes in handy in many situations. You can use it to spawn enemies or food at different locations in the game. You can also use it to suggest random passwords to users or create filenames to save files. I wrote a tutorial on how to generate random alphanumeric strings in PHP. I said at the beginning of this post that few events are truly random, and the same applies to random number or string generation. In this tutorial, I'll show you how to generate a pseudo-random alphanumeric string in JavaScript. Generating Random Numbers in JavaScript Let’s start by generating random numbers. The first method that comes to mind is Math.random(), which returns a float

Fractions and percentages are two basic mathematical concepts used in various fields such as finance, data analysis, and statistics. Converting a fraction to a percentage is a simple but necessary operation that allows us to represent fractional values in a more understandable way. Python, one of the most popular programming languages, offers several methods for converting fractions to percentages, and understanding these methods is crucial for anyone working with data in Python. This article takes an in-depth look at converting fractions to percentages in Python. We'll explore different ways to do this and provide examples to help you understand each method. Whether you are a beginner or an experienced Python programmer, this article gives you the key to doing this in Python

The difference between random and pseudo-random is predictability, reproducibility, uniformity and security. Detailed introduction: 1. Predictability. Random numbers cannot be predicted. Even if the past results are known, future results cannot be accurately predicted. Pseudo-random numbers can be predicted because they are generated by algorithms. As long as you know the algorithm and seed, you can regenerate the same sequence or sequence; 2. Reproducibility, random numbers are not reproducible, and the results generated each time are independent, while pseudo-random numbers are reproducible. Yes, just use the same algorithm and seeds etc.

In this article, we will learn how to get word frequency as a percentage in Python. Suppose we have obtained a list of string inputs. Now, we will find the percentage of each word in the given list of input strings. The formula (OccurrenceofXword/Totalwords)*100 uses the sum(), Counter(), join() and split() functions. Use the join(), split() and count() functions. Use the countOf() function of the operator module. Method 1: Use sum(), Counter(), join() and split() functions join() is Py

Python is a popular programming language widely used in data analysis, machine learning, web development and other fields. In Python, the random module provides a random.randint() function for generating random integers. This article explains how to use the random.randint() function to generate random integers and provides some code examples. First, we need to import the random module: importrandom Next, we can call rando

In a Tencent meeting, users can turn on their own cameras for sharing, but the camera has a maximum number of people. So what is the upper limit of the number of people on the camera? Below, the editor will bring you an introduction to the maximum number of people in the Tencent meeting camera. Let’s find out together. Bar. Tencent meeting usage tutorial: What is the maximum number of people for Tencent meeting camera? Answer: The maximum is 60 people and the minimum is 30 people. 1. Introduction to the upper limit of the number of people in the camera: 1. The computer can accommodate up to 60 people who can turn on the camera, and a screen can accommodate up to 9 or 25 people at the same time. 2. The mobile phone can support up to 30 people turning on the camera, and one screen can accommodate up to 4 people at the same time. 2. Methods to increase the upper limit of the number of videos: 1. If there are too many participants, up to 30 people can turn on the camera at the same time. 2. We can use the account avatar interface to

Press 1 to see the usage of each CPU. The first line is the current time. The running time is the currently logged in user. Load balancing (1 minute, 5 minutes, 10 minutes). The loadaverage data checks the number of active processes every 5 seconds and calculates the value. If the number divided by the number of logical CPUs exceeds 5, the system is overloaded. The second line has a total of 248 processes, 1 running, 247 sleeping, 0 stopped, 0 zombie processes. The third line us (userspace): the percentage of cpu occupied by user space sy (sysctl): the percentage of cpu occupied by kernel space ni()— The percentage of CPU occupied by the process that has changed the priority id (idolt): idle CPU percentage wa (wait): IO waiting occupied CP

How to Show Battery Percentage on iPhone Previously, you had to swipe down to open Control Center to find the exact percentage of remaining battery power. However, the option to see the precise battery percentage on the top edge of the lock screen is back. To show battery percentage on your iPhone: Open the Settings app from your iPhone home screen. Swipe down the Settings menu and tap Battery from the list. Next, switch the battery percentage button to the on position. Finally, you'll see the exact percentage of remaining power above the battery icon in the upper right corner of the screen. When the feature is enabled, percentage numbers will appear when you browse the Lock screen, Home screen, and most apps. Can't see battery percentage? If you don't see the
