PHP implementation of probabilistic random lottery code _php Tips

Source: Internet
Author: User

1. Initial data:

The greater the weight, the higher the probability of extraction
[Prize 1, Weight 5], [Prize 2, Weight 6], [Prize 3, Weight 7], [Prize 4, Weight 2]

2, processing steps:

1) N = 5 + 6 + 7 + 2 = 20
2) Then take the random number of 1-n m
3) to define the weight range value of each prize prizes 1:1-5; Prizes, 2:6-11, prizes, 3:12-18, prizes 4:19-20
4 if M is within the weight range of a prize, mark the prize as being extracted to

<?php/** * Prize/class Prize {# ID public $id = null;
  # weight public $weight = null;
 
  # prize name Public $name = NULL;
  # Weight range interval starting value protected $start = 0;
 
  # Weight range interval End value protected $end = 0;
    Public function __construct ($id, $weight, $name) {if (! $id) {throw new Exception (' Trophy ID empty. ')
    } $this->id = $id; $this->weight = $weight?
    $weight: 0; $this->name = $name? $name: ' Random prizes '.
  $id;
  # ID Public Function getId () {return $this->id;
  # Weight Public Function getweight () {return $this->weight;
    # Set Weights range Public Function SetRange ($start, $end) {$this->start = $start;
  $this->end = $end; # to determine whether random numbers are in the range of weights public Function InRange ($num) {return ($num >= $this->start) && ($num <= $t
  His->end);
 
  }/** * Prize pool/class Prizepoll implements Iteratoraggregate, countable {# Trophy collection protected $items = Array (); # Join the prize public Function AddItem (Prize $Item) {$this->items[$item->getid ()] = $item;
  return $this; # Delete Prizes public function RemoveItem ($itemId) {if (Isset ($this->items[$itemId))) {unset ($this->item
    s[$itemId]);
  return $this; # Update Prizes public Function UpdateItem (Prize $item) {if isset ($this->items[$item->getid ())) {$this-
    >items[$item->getid ()] = $item;
  return $this;
  # Get all Prizes public Function GetItems () {return $this->items;
    # All available Prizes (if the weight is 0, which means this trophy will never be drawn) public function Getvisibleitems () {$items = array ();
      foreach ($this->items as $item) {if ($item->getweight ()) {$items [$item->getid ()] = $item;
  } return $items;
  # Countable::count Public Function count () {return count ($this->items);
  # iteratoraggregate::getiterator () Public function Getiterator () {return new Arrayiterator ($this->items); }/** * Simple lottery Category * * Class SimpLeturn {# prize pool protected $poll = null;
    Public function __construct (Prizepoll $poll) {if ($poll) {$this->setpoll ($poll);
    # Draw Public Function run (Prizepoll $poll) {$poll = $poll? $poll: $this->poll;
    if (! $poll) {throw new Exception (' award pool not initialized ');
    if ($poll->count () <= 0) {throw new Exception (' award pool is empty ');
    $items = $poll->getvisibleitems ();
    if (count ($items) <= 0) {throw new Exception (' award pool is empty ');
    } $sum = 0;
      foreach ($items as $item) {$start = $sum + 1;
      $sum + + $item->getweight ();
 
      $end = $sum;
    # Set the weight range of prizes $item->setrange ($start, $end);
 
    # random Number $rand = $this->getrandnum (1, $sum);
      # interval section to determine foreach ($items as $item) {if ($item->inrange ($rand)) {return $item;
  } return null;
# get random number Public function Getrandnum ($min, $max) {return Mt_rand ($min? $min: 1, $max);  # set up the bonus pool public function Setpoll (Prizepoll $poll) {$this->poll = $poll;
  # example try {$prizePoll = new prizepoll (); $prizePoll->additem (New Prize (1, 5))->additem (New Prize (2, 6))->additem (New Prize (3, 7))->addit
 
  EM (New Prize (4, 2));
  $turn = new Simpleturn ($prizePoll);
  $prize = $turn->run ();
Var_dump ($prize);
 catch (Exception $e) {print_r ($e);}

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.