start

The thing is, my girlfriend is crazy about the price of Pduo Duo, and she always asks me to help her with some things, such as mobile phone, tablet balancing car, lipstick, perfume and goose (X2). She also asks me to invite other people to help her with the price. I am very reluctant in my heart, but how can I explain to her that this is not possible? In order to explain the principle of a knife, SO I stayed up late to write a page to bargain, simulating the process of inviting friends to help bargain.

Take a look at the finished page

after

A total of 5 pages, the first page is the commodity haggling progress page, here through a progress bar display haggling progress, the following display is haggling record, every time someone cut a knife, here will be more than a record, showing who help you cut how much. There is an invite button below the progress bar. Click this button to share the current page with your friends. Because it is H5 page, I did not call the sharing SDK, but after clicking the button, automatically copy the sharing link to the paste board, you can share to the seven aunts and eight aunts through any way (wechat, QQ, etc.), in fact, you can not directly share the link to wechat, but share a password.

Then open wechat and share the link with someone:

Two dogs click on the link, it opens the second page:

In this page, two dogs can see you let him help you cut 10 clean and haggling progress, as a good gay friend of both sides of the rib, two dogs, of course, no words, click the haggling button, and then appear the third page (in fact, there is no this page in the fight xi Xi, I deliberately added in order to effect)

This page will have a haggling animation and sound effects, just to make your hacking more exciting. Then a fourth page appears:

Two dogs in a meal after operation, finally help you cut 0.03 yuan

But bao10 Jie, worth 123, still needed 0.62 points to drive home for free. I couldn’t give up, so I invited the chicken, Biao Zi, Yong Qiang, Erya, old Ribs and other friends to make efforts and successfully negotiated the price before 0 PM:

This is the whole bargaining process. However, when we observe the record of haggling, we will find that the more the amount cut, the less the remaining amount, and the amount we can see each time is less and less, but not absolutely. Occasionally, a few people can cut a relatively high amount, but the overall trend is lower and lower:

How does this work? Is it completely random to see how much you bargain with each time?

GC

In fact, yes and no, the number of cuts each time depends on the probability.

If the remaining amount is x, and the amount that can be cut each time is x*y, then the size of the y value determines how much can be cut each time. We make the following rules:

  • The value of y ranges from 0.1 to 0.9
  • 0.1 has a 90 percent chance of occurring, 0.2 has half the chance, or 45 percent, and so on, until 0.9 has a 0.3515625 percent chance
  • The minimum value for each bargain is 0.01
  • Bargaining is limited to 24 hours
  • Each user can only bargain once
  • When the remaining amount is 0 within 24 hours, the bargaining is successful

The condition is clear, you can achieve the haggling algorithm, here we do not consider the time and whether the same user judgment, only calculate the amount of each haggling.

The default y value is 0.1, and then generate a random number. Determine the size of the random number to calculate the probability. We know that Math.random can generate a random number of 0 and 1. For example, what is the probability of 10%? By default, random numbers are averaged between 0 and 1. What is the probability that this number is less than 0.1? Yes, 10%, or 10% probability of greater than 90%, or even 10% probability of random numbers appearing in any range of 0.1, which is greater than 0.4 and less than 0.5 is 0.1.

Ok, 10% of the time, 90% of the time? The probability of a random number r < 0.9 or r > 0.1 is 90%, so we can represent any probability.

As shown in the following code, the probability of occurrence of 0.1 is 90%, which is represented by R < 0.9; the probability of occurrence of 0.2 is halved, which is 45%, which is represented by R < 0.45; the probability of occurrence of 0.3 is halved again, which is represented by R < 0.225; one analogy, and finally the probability of occurrence of 0.9 is 0.3515625%. R < 0.003515625.

// y The default value is 0.1
let y = 0.1;
// random number r, used to calculate the probability
let r = Math.random()
switch (true) { 
  case r < 0.003515625: y = 0.9;break;
  case r < 0.00703125: y = 0.8;break;
  case r < 0.0140625: y = 0.7;break;
  case r < 0.028125: y = 0.6;break;
  case r < 0.05625: y = 0.5;break;
  case r < 0.1125: y = 0.4;break;
  case r < 0.225: y = 0.3;break;
  case r < 0.45: y = 0.2;break;   //r is a random number between 0 and 1. 45% probability is less than 0.45
  case r < 0.9: y = 0.1;break;   //r is a random number between 0 and 1. The probability of r being less than 0.9 is 90%
  default: y = 0.1;
}

// This value can be used to further adjust the amount of each bargain
const ratio = 0.5;
// The total price minus the cut price equals the remaining price
let kp = (goods.total - goods.already) * ratio * y
// Set the minimum haggling value to 0.01
kp = kp < 0.01 ? 0.01 : kp;
Copy the code

Obtained after y, using price multiplied by the remaining y can bargain amount every time, but in order to increase the difficulty of bargaining, I have added a coefficient thewire, this is a constant, by setting the size of this value can be further control the difficulty of the bargain, here I set to 0.5, for example, makes time bargain amount on the basis of the previous halved again, This doubles the number of times you haggle, and you need to share it with more friends.

The results of

So you see, you’ve been hacking all day, and the key to success is just a number in the background.

Stay up all night, finally finished writing the code, looking at the finished demo, I completely no sleepy, just waiting for my girlfriend to wake up to tell her.

After the

The above code can be obtained by searching H5talks reply PDD on wechat