Fortune Telling Collection - Horoscope - WeChat Red Packet 5 yuan Random Red Packet Random Amount

WeChat Red Packet 5 yuan Random Red Packet Random Amount

Detailed explanation and algorithm code of WeChat red envelope random number _ WeChat red envelope random number

1 demand

Write? Fixed red envelope+random red envelope

A fixed red envelope is every red envelope? Huh? Sample, how many fixed red envelopes do you have? Uh, just? .

The demand for random red envelopes is. ? Like red envelopes? Er, it's 5 yuan, and you need to send a 10 red envelope. The random range is 0.0 1 to 0.99; Five dollars must be paid out. Uh, do you need it? Normal distribution with constant trend.

(0.99 can be specified at will, or it can be AVG * 2-0.01; ? Such as avg = 5/10 = 0.5; ; (average * 2-0.0 1 = 0.99))

2 demand analysis

2. 1 Fixed red envelope

If it is a fixed red envelope, what is the algorithm? Straight line. T is the amount of a fixed red envelope. As shown in the figure.

f(x)= t; ( 1 & lt; = x < = number)

image.png

2.2 random red envelopes

What if we do it? Random function rand. Rand (0.0 1, 0.99); So 10 times random, if the worst case is all? Amount 0.99, total? Uh, 9.9 yuan. Will surpass 5 yuan. ? The amount will also be abnormally distributed. Finally thought of it? With the help of sum mathematical function, can it be used as a random red envelope? Equipment, okay? Parabola, three? Function. Finally, I chose isosceles three? Linear function.

1 algorithm principle

If you need a red envelope, always? The total amount is Money, and the number of red envelopes is num. The range of forehead is [minimum, maximum], linear? The process is shown in the figure.

image.png

Coordinates of three points:

(x 1,y 1) = ( 1,min)

(x2, y2) = (quantity /2, maximum)

(x3, y3) = (number, minimum value)

Deterministic linearity? Cheng:

$ y = 1.0 *(x-$ x 1)/($ x2-$ x 1)*($ y2-$ y 1)+$ y 1; (x 1 & lt; = x & lt= x2)

$ y = 1.0 *(x-$ x2)/($ x3-$ x2)*($ y3-$ y2)+$ y2; (x2 & lt= x & lt= x3)

Maintenance data:

Y (total) = y1+y2+y3+... ynum;

Y (combination) may be > TotalMoney, explain? Become? If the number is too large and the data needs to be modified, multiply it by 0.0 1, y2, y3...ynum. Until y (total) = totalMoney.

Y (together) may be < TotalMoney, explain? Become? If the number is small and the data needs to be modified, add 0.01,y2, y3) from (y 1 ... ynum) each time. Until y (total) = totalMoney.

2 example of algorithm principle

If you need a red envelope, always? The amount is 1 1470, and the number of red envelopes is 7400. The range of the amount is [0.0 1, 3.09], linear? The process is shown in the figure.

image.png

3 Demand design

3. 1 class diagram design

image.png

3.2 Source Code Design

/**

* random red envelope+fixed red envelope algorithm [strategy mode]

*/

//Configure DTO for transmitting data.

Category options

{

//Red envelopes? forehead

Total public funds;

//Number of red envelopes

Public $ num

//range start

public $ rangeStart

//Range settlement

public $ rangeEnd

//? Red envelope strategy

public $ builderStrategy

//Random Red Packet Residual Rule

Public $ rand format type//can _ left: data can be left without repair; No_Left: there cannot be a public static function create ($ totalmoney, $ num, $ rangestart, $ rangeend, $ builder strategy, $ rand format =' no _ left').

{

$self = new self ();

$ self->; num = $ num

$ self->; rangeStart = $ rangeStart

$ self->; rangeEnd = $ rangEnd

$ self->; totalMoney = $ totalMoney

$ self->; builder strategy = $ builder strategy;

$ self->; randFormatType = $ randFormatType;

Return to $ self

}

}

//Red envelopes? Complete the device connection?

Interface construction strategy

{

//Create a red envelope

Public function create ();

//Set the configuration

Common function setoption (options to $ option);

//Is that all right? Turn into a red envelope

Common function iscanbuilder ();

//? Red envelope function

Common function FX ($ x);

}

//Fixed equal red envelope strategy

EqualPackageStrategy class implements IBuilderStrategy {

//A single red envelope? forehead

Public $ oneMoney

//quantity

Public $ num

public function _ _ construct($ option = null)

{

if($ option instance of options to)

{

$ this-& gt; setOption($ option);

}

}

Common function setOption(options to $ option)

{

$ this-& gt; one money = $ option-& gt; rangeStart

$ this-& gt; num = $ option-& gt; num

}

Public function create ()

{

$ data = array();

if(false = = $ this-& gt; isCanBuilder())

{

Return to $ data

}

$ data = array();

if(false = = is _ int($this-& gt; num)| | $ this-& gt; num & lt= 0) {

Return to $ data

}

for($ I = 1; $ i & lt= $ this-& gt; num$i++)

{

$ data[$ I]= $ this-& gt; Foreign exchange (USD);

}

Return to $ data

}

/**

* Equal red envelopes? Cheng is? straight line

*

* @param mixed $x

* @ Access to the public

* @ returns void

*/

Common function fx($x)

{

return $ this-& gt; oneMoney

}

/**

* Can you handle the red envelope?

*

* @ Access to the public

* @ returns void

*/

Common function isCanBuilder ()

{

if(false = = is _ int($this-& gt; num)| | $ this-& gt; num & lt= 0)

{

Returns false

}

if(false = = is _ numeric($this-& gt; one money)| | $ this-& gt; oneMoney & lt= 0)

{

Returns false

}

//A single red envelope? At 1

If ($ this->; oneMoney & lt0.0 1)

{

Returns false

}

Return true

}

}

//Random red envelope strategy (3? Shape)

RandTrianglePackageStrategy class implements IBuilderStrategy

{

//Total amount

Total public funds;

//Number of red envelopes

Public $ num

//What is the most random red envelope? value

Public $ TERM min money

//What is the most random red envelope? value

Public $ maxMoney

//Repair data? Formula: NO_LEFT: total amount of red envelopes = total budget; CAN_LEFT: total amount of red envelopes.

//Budget surplus? forehead

public $ leftMoney

public function _ _ construct($ option = null)

{

if($ option instance of options to)

{

$ this-& gt; setOption($ option);

}

}

Common function setOption(optionadto $ option){) (

$ this-& gt; total money = $ option-& gt; Total amount of money;

$ this-& gt; num = $ option-& gt; num

$ this-& gt; format type = $ option-& gt; randFormatType$ this-& gt; min money = $ option-& gt; rangeStart

$ this-& gt; maxMoney = $ option-& gt; rangeEnd

$ this-& gt; left money = $ this-& gt; Total amount of money;

}

/**

* Create random red envelopes

*

* @ Access to the public

* @ returns void

*/

Public function create ()

{

$ data = array();

if(false = = $ this-& gt; isCanBuilder())

{

Return to $ data

}

$ left money = $ this-& gt; leftMoney

for($ I = 1; $ i & lt= $ this-& gt; num$i++)

{

$ data[$ I]= $ this-& gt; Foreign exchange (USD);

$ left money = $ left money-$ data[$ I];

}

//repair data

list($okLeftMoney,$ ok data)= $ this-& gt; Format ($leftMoney, $ data);

//Random sorting

shuffle($ ok data);

$ this-& gt; leftMoney = $ okLeftMoney

Return to $ okData

}

/**

* Can I give out red envelopes at random?

* @ Access to the public

* @ returns void

*/

Common function isCanBuilder ()

{

if(false = = is _ int($this-& gt; num)| | $ this-& gt; num & lt= 0)

{

Returns false

}

if(false = = is _ numeric($this-& gt; total money)| | $ this-& gt; totalMoney <= 0) {

Returns false

}

//average value

$ avg money = $ this-& gt; total money/ 1.0/$ this-& gt; num

//mean? Most? value

if($ avg money & lt; $ this-& gt; minMoney)

{

Returns false

}

Return true

}

/**

* Get the rest? forehead

* @ Access to the public

Public function getLeftMoney ()

{

return $ this-& gt; leftMoney

}

/**

* Random red envelopes? Into a function. Three? Function. [( 1,0.0 1),($num/2,$avgMoney),($num,0.0 1)] * @param mixed $x, 1 & lt; = $ x & lt= $ this-& gt; num

* @ Access to the public

* @ returns void

*/

Common function fx($x)

{

if(false = = $ this-& gt; isCanBuilder())

{

Returns 0;

}

if($ x & lt; 1 | | $ x & gt; $ this-& gt; Numbers)

{

Returns 0;

}

$ x 1 = 1;

$ y 1 = $ this-& gt; Mimoni;

//intermediate point

$ x2 = ceil($ this-& gt; num/ 1.0/2);

//My peak

$ y2 = $ this-& gt; maxMoney

//Last point

$ x3 = $ this-& gt; num

$ y3 = $ this-& gt; Mimoni;

//When X 1, X2 and X3 are both 1 (vertical lines).

if($ x 1 = = $ x2 & amp; & amp$x2 == $x3)

{

//'/_ \' Three? Linear? travel

//'/'section

if($x 1! = $ x2 & amp& amp$ x & gt= $ x 1 & amp; & amp$ x & lt= $x2)

{

、y = 1.0 *(x-$ x 1)/($ x2-$ x 1)*($ y2-$ y 1)+$ y 1; Returns number_format($y, 2,'.','');

}

//'\' shape

if($x2! = $ x3 & amp& amp$ x & gt= $ x2 & amp& amp$ x & lt= $x3)

{

$ y = 1.0 *(x-$ x2)/($ x3-$ x2)*($ y3-$ y2)+$ y2; Returns number_format($y, 2,'.','');

}

Returns 0;

}

/**

* format to repair red envelope data

*

* @param mixed $leftMoney

* @param array $ data

* @ Access to the public

* @ returns void

*/

Private function format ($leftMoney, array $data)

{

//You can't just give out red envelopes.

if(false = = $ this-& gt; isCanBuilder())

{

Returns an array ($leftMoney, $ data);

}

//The remaining red envelopes are 0.

if(0 == $leftMoney) //? Need maintenance data

Returns an array ($leftMoney, $ data);

}

//The array is empty

if(count($ data)& lt; 1)

{

Returns an array ($leftMoney, $ data);

}

//If there is a surplus and $ leftmoney >; 0

if(' Can _ Left ' = = $ this-& gt; Format type

& amp& amp$ leftMoney & gt0)

{

Returns an array ($leftMoney, $ data);

}

//If there is any money left, try to add it to? Red envelopes? Try it if you don't add it? a . while($left money & gt; 0)

{

$ found = 0;

foreach($ data as $ key = & gt; $val)

{

//Reduce cycle optimization

if($ left money & lt; = 0)

{

Break;

}

//prejudge

$ afterLeftMoney =(double)$ left money-0.0 1;

$ after val =(double)$ val+0.0 1;

if($ afterLeftMoney & gt; = 0 & amp& amp$ afterVal & lt= $ this-& gt; maxMoney)

{

$ found = 1;

$ data[$ key]= number _ format($ after val,2,'.','');

$ leftMoney = $ afterLeftMoney

//Accuracy

}

}

//If there is no red envelope to add, you need to finish it, otherwise the infinite loop ends.

//In other words, every red envelope won't have money. For example, red envelopes are already the most? Value. At this time, you must give a sign when you part. Infinite loop. if($found == 0)

{

Break;

}

}

//If $ leftmoney

while($left money & lt; 0)

{

$ found = 0;

foreach($ data as $ key = & gt; $val)

{

if($ left money & gt; = 0)

{

Break;

}

//prejudge

$ afterLeftMoney =(double)$ left money+0.0 1;

$ after val =(double)$ val-0.0 1;

if($ afterLeftMoney & lt; = 0 & amp& amp$ afterVal & gt= $ this-& gt; minMoney)

{

$ found = 1;

$ data[$ key]= number _ format($ after val,2,'.','');

$ leftMoney = $ afterLeftMoney

$ left money = number _ format($ left money,2,'.','');

}

}

//If? If there is no reduced red envelope, it needs to end, otherwise it is an infinite loop.

if($found == 0)

{

}

}

Returns an array ($leftMoney, $ data);

}

}

//Maintain the environment class of the policy.

RedPackageBuilder class

{

//Example

Protected static $ _ instance = null

/**

* Single instance (obtained instance)

*

* @return MemcacheOperate

*/

Public static function getInstance ()

{

if (null === self::$_instance)

{

self::$ _ instance = new self();

}

Return self::$ _ instance;;

}

/**

* Develop a strategy? show

*

* @param string $type type

* @ returns void

*/

Public function getBuilderStrategy($type) {

$class = $type。 "packaging strategy";

if(class_exists($class))

Returns a new $ class ();

}

other

{

Thrownnewexception ("{$ class} class does not exist! " );

}

}

The public function getredpackagebydto (option adto $ option dto)

{

//get policy

$ builder strategy = $ this-& gt; getBuilderStrategy($ options to-& gt; builder strategy); //Set parameters

$ builder strategy-& gt; setOption($ option dto);

return $ builder strategy-& gt; create();

}

}

Category client

{

Common static function main($argv)

{

//Fixed red envelope

$ dto = option dto:: create (1000,10, 100, 100,' equal to');

$ data = RedPackageBuilder::getInstance()-& gt; getRedPackageByDTO($ dto);

//print _ r($ data);

//Random Red Packet [Repair Data]

$dto = OptionDTO::create(5, 10,0.0 1,0.99,' rand triangle ');

$ data = RedPackageBuilder::getInstance()-& gt; getRedPackageByDTO($ dto); print _ r($ data);

//Random red envelope [No data repair]

$dto = OptionDTO::create(5, 10,0.0 1,0.99,' RandTriangle ',' Can _ Left ');

$ data = RedPackageBuilder::getInstance()-& gt; getRedPackageByDTO($ dto);

//print _ r($ data);

}

5.9

Baidu library VIP limited time discount is now open, enjoy 600 million +VIP content.

Go get it now

Detailed explanation and algorithm code of WeChat red envelope random number _ WeChat red envelope random number

Detailed explanation and algorithm code of WeChat red envelope random number _ WeChat red envelope random number

1 demand

Write? Fixed red envelope+random red envelope

A fixed red envelope is every red envelope? Huh? Sample, how many fixed red envelopes do you have? Uh, just? .

The demand for random red envelopes is. ? Like red envelopes? Er, it's 5 yuan, and you need to send a 10 red envelope. The random range is 0.0 1 to 0.99; Five dollars must be paid out. Uh, do you need it? Normal distribution with constant trend.

(0.99 can be specified at will, or it can be AVG * 2-0.01; ? Such as avg = 5/10 = 0.5; ; (average * 2-0.0 1 = 0.99))

Page 1

2 demand analysis

2. 1 Fixed red envelope

If it is a fixed red envelope, what is the algorithm? Straight line. T is the amount of a fixed red envelope. As shown in the figure.

f(x)= t; ( 1 & lt; = x < = number)

image.png

2.2 random red envelopes

What if we do it? Random function rand. Rand (0.0 1, 0.99); So 10 times random, if the worst case is all? Amount 0.99, total? Uh, 9.9 yuan. Will surpass 5 yuan. ? The amount will also be abnormally distributed. Finally thought of it? With the help of sum mathematical function, can it be used as a random red envelope? Equipment, okay? Parabola, three? Function. Finally, I chose isosceles three? Linear function.

page 2

1 algorithm principle

If you need a red envelope, always? The total amount is Money, and the number of red envelopes is num. The range of forehead is [minimum, maximum], linear? The process is shown in the figure.

image.png

Coordinates of three points:

(x 1,y 1) = ( 1,min)

(x2, y2) = (quantity /2, maximum)

(x3, y3) = (number, minimum value)

Deterministic linearity? Cheng:

$ y = 1.0 *(x-$ x 1)/($ x2-$ x 1)*($ y2-$ y 1)+$ y 1; (x 1 & lt; = x & lt= x2)

Page 3

$ y = 1.0 *(x-$ x2)/($ x3-$ x2)*($ y3-$ y2)+$ y2; (x2 & lt= x & lt= x3)

Maintenance data:

Y (total) = y1+y2+y3+... ynum;

Y (combination) may be > TotalMoney, explain? Become? If the number is too large and the data needs to be modified, multiply it by 0.0 1, y2, y3...ynum. Until y (total) = totalMoney.

Y (together) may be < TotalMoney, explain? Become? If the number is small and the data needs to be modified, add 0.01,y2, y3) from (y 1 ... ynum) each time. Until y (total) = totalMoney.

Page 4

2 example of algorithm principle

If you need a red envelope, always? The amount is 1 1470, and the number of red envelopes is 7400. The range of the amount is [0.0 1, 3.09], linear? The process is shown in the figure.

image.png

3 Demand design

3. 1 class diagram design

image.png

3.2 Source Code Design

/**

* random red envelope+fixed red envelope algorithm [strategy mode]

Page 5

*/

//Configure DTO for transmitting data.

Category options

{

//Red envelopes? forehead

Total public funds;

//Number of red envelopes

Public $ num

//range start

public $ rangeStart

Page 6

//Range settlement

public $ rangeEnd

//? Red envelope strategy

public $ builderStrategy

//Random Red Packet Residual Rule

Public $ rand format type//can _ left: data can be left without repair; No_Left: there cannot be a public static function create ($ totalmoney, $ num, $ rangestart, $ rangeend, $ builder strategy, $ rand format =' no _ left').

Page 7

{

$self = new self ();

$ self->; num = $ num

$ self->; rangeStart = $ rangeStart

$ self->; rangeEnd = $ rangEnd

$ self->; totalMoney = $ totalMoney

$ self->; builder strategy = $ builder strategy;

$ self->; randFormatType = $ randFormatType;

Return to $ self

Expand the full text?

Read the full text for free in the application.

Go to the library APP to sign in and read the card, and read the WeChat red envelope random number for free. ...

Restrictive exemption

Guide length diagram

Transfer to network disk

Send to wechat

Download document

Beijing Baidu Netcom Technology Co., Ltd. 8.0.70 Privacy Policy Permission Description

Copyright: This document is provided and uploaded by users, and the income belongs to the content provider exclusively. If there is any infringement of the content, please report or claim.

Page 8

Randomly draw the WeChat red envelope number for you ... 964 member documents.

Simulation and Application of Random Amount Generation Algorithm for WeChat Red Packet

2537 people read

Simulation and Application of Random Amount Generation Algorithm for WeChat Red Packet

1455 people read

Is the amount of WeChat red packets really randomly allocated?

239 1 person reading

Analysis and Implementation of WeChat Red Packet Algorithm

2508 people read

Get all the documents. 4326 people are watching

According to your browsing, I will organize a data set for you.

Detailed explanation and algorithm code of WeChat red envelope random number _ WeChat red envelope random number

FOLDER

Analysis on the Law of WeChat Red Packet —— Baidu Library

3.6 points

2906 reading

Popular and well written

How to guess the mantissa of a five-piece, five-bag red envelope-Baidu Library

4.4 points

1082 reading

Simulation and Application of Random Amount Generation Algorithm for WeChat Red Packet —— Baidu Library

4.0 points

149 1 reading

The number of downloads has soared recently.

10 selected documents remaining

Go to the APP to get all the collections with one click.

relevant documents

Simulation and Application of Random Amount Generation Algorithm for WeChat Red Packet

Get the full text for free

Simulation and Application of Random Amount Generation Algorithm for WeChat Red Packet

Get the full text for free

Is the amount of WeChat red packets really randomly allocated?

Get the full text for free

Analysis and Implementation of WeChat Red Packet Algorithm

Get the full text for free

Wechat red envelope digital meaning daquan courseware. document

2567 people have seen it.

Industry praise

Principle and Application of Microcomputer System Test Database and Answers (Editable)

2060 people have read it

one in a hundred

Bidding for computer network system (technical part)

1569 people have seen it.

Network security equipment-gateway [finishing version]

1 108 people have seen it.

Four methods of exporting Excel data with ASP

22 13 people have seen it.

Scheme of developing network security education activities in schools

1822 people have seen it.

The third comprehensive application comprehensive case The fifth comprehensive case of computer network application

2470 people have seen it.

How to weld a broken CPU pin?

12 18 people have seen it.

Searching, Inserting and Deleting Data Structure Sequence Table

1594 people have seen it.

Implementation scheme of network security construction

133 1 people have seen it

Network security inspection self-inspection form

1489 people have seen it.

Read more

Recommended document set