After a year of hard work, large and small companies or organizations will hold an annual meeting. The annual meeting is not a lottery, everyone is looking forward to their first prize, but the reality is cruel, I did not draw this year, hair are not a root. Why is that? Let’s talk about the lottery algorithms. Annual lottery, in accordance with the form, can be divided into physical lottery and program random algorithm.
Physical lottery,
Everybody knows this, have a raffle box, everyone enters the entrance write their name on a piece of paper, fold it up, drop it into the box. Then your fate is in the hands of the leaders who take the stage. The lottery must be drawn from the bottom of the worst first, some companies or units of the organization is the fifth prize, some direct third prize, there is no way, have to pay attention to the budget. This kind of lottery situation, if the host does not shake a shake, and rule out the leadership of the comrade like seabed fishing addiction, then the more back into the box of friends are more likely to be selected. But don’t lean on the queen either. I know you want to get drawn, but you don’t want to get drawn early. If the third prize is a ticket to a movie, you might as well win the prize. On the other hand, if the third prize in a company is a movie ticket, you wait until the first prize, there is no expectation, perhaps a toothbrush thunder death you don’t pay.
Program lottery,
Another form, that is the program draw. Program draw lots, is to write an electronic lottery program, to achieve a random algorithm to scientific means to draw. Big name, kind of scientific fortune-telling. Let’s take the cheating out of the programmer’s mind and think about whether the random algorithms they write are fair or not. The general process of electronic lottery is to let the leader call start, after a random period of time, the leader call stop. In this process, the program only has these two input control commands. And, for the sake of effect, there is usually a random scroll on the big screen of the faces or names of colleagues who entered the lottery. So there are two questions, which faces do YOU choose to scroll on the big screen? After the roll, how to draw n (n>1) winners at random, and from where?
Scroll process head selection algorithm
Let’s say there are about 1,000 people in the lottery. Each time the big screen can only display a maximum of 9 (nine grid, good-looking).
Lazy programmers choose algorithms
According to the order of sign-in first come last, 9 colleagues are extracted from the front to the wall each time. Until the big man says stop. That’s great. For those of you who want to show up, check in early for last year’s event. If you’re late, you’ll be lost to the sea. The programmer who wrote this must be thinking, 1000 people at a time to read from the database, afraid of performance problems, or a previously written paging interface, just come in handy.
Selection algorithms for diligent programmers
He would think hard about fairness. To save that little bit of performance, read the profile pictures of all 1,000 colleagues at once before the draw, trading space for time. Then call the random number function 9 times, according to the result of 9 random numbers from the set. After the extraction is removed from the set, reordered, the total as the next random range, until the big guy says stop. If the round is over, the big guy plays heart big, has not shouted stop. Then put the eliminated colleagues back into the set, like a poker shuffle. For those of you who know a little about the program, the random function here uses some of the system’s own random functions, such as javascript. Doesn’t that seem a little fair?
When the big man finally called stop, it started the lottery random algorithm, select the winner. You got to the wall, but you didn’t get the draw. That’s no good. So the key is to see how the programmer implements the lottery algorithm. Even if you don’t hit the wall, don’t be discouraged. Chances are, it depends on what kind of programmer you meet.
Lottery algorithms for unlicensed programmers
Select n winners from the participants on the front wall, n must be less than the total number of participants on the wall. Run into such, still be that sentence, call you early last year meeting spot, this next suffer a loss? In addition, some leaders like to stop at the beginning, the big screen has not even rolled, so you should go to the scene early.
Reliable programmer’s lottery algorithm
First, he understood that the collection to be raffled must not be drawn solely from the upper collection, which was merely a gimmick, akin to a cheerleading stunt during a basketball timeout. Have you ever seen a cheerleader pick an MVP? So the sample space is still 1,000 participants. In fact, it is also very simple, using the system random number, from the range of 1000 to extract N serial number, according to the serial number to find the winner. That way, it doesn’t matter whether you come in early or late.
As mentioned, the raffles are drawn round by round. So, at the very least, make sure you eliminate the winners from the pool after each round. Otherwise a person luck burst, ancestral grave smoke all the awards have taken, you can not blame him cheating.
Internal algorithms for random functions?
If you are interested, you can Google the random method to see how it returns a random number and whether it is fair or not. If so, be sure to share your comments here.
Finally, I wish you guys and sisters a big prize in the annual conference. Don’t encounter unlucky programmers and unlicensed programmers.