PHP彩票候选人选择器

时间:2015-07-12 19:00:13

标签: php mysql

我即将创建一个脚本,为我的彩票选择一个胜利者。门票数量的选择如下:amount of money * 100所以$ 1.26 = 126门票。

我做了这个,它给了我中奖的票号,但后来我无法得到获胜的用户:

$totaltickets = 0;

foreach($players as $player){

    $totaltickets += $player->depositedValue*100;
}

$winningTicket = rand(1,$totaltickets);

我有这样的行:

Player1 - 1.25$
Player2 - 5.99$
etc..

如果有可能,那么我希望保持这样,并且数据库中每张票都没有1000行。

4 个答案:

答案 0 :(得分:3)

您可以使用此代码:

<?php

    function getWinnerPlayer($players) {
        /* get total amount of tickets */
        $total_tickets = 0;
        foreach ($players as $player) {
            /* var_dump($player->depositedValue); */
            $total_tickets += $player->depositedValue * 100;
        }

        /* get winner ticket */
        $winner = rand(1, $total_tickets);

        /* return winner player */
        $count = 0;
        foreach ($players as $player) {
            // $total_tickets is not the correct variable, sorry
            // $count += $total_tickets->depositedValue * 100;
            $count += $player->depositedValue * 100;
            if ($count >= $winner) return $player;
        }
    }

?>

答案 1 :(得分:2)

我正在考虑保留你对数字的想法,而不是引入数组。

我将让球员保持他们的门票位置(开始/结束)。当我选择随机票时,我会看看我的号码是否在他们的范围内,如果是,那么我找到了胜利者。

<?php

class TicketMaster {
    private $players = array();
    public $total = 0;

    public function addPlayer($player) {
        $player->tickets[0] = $this->total;
        $this->total += $player->value;
        $player->tickets[1] = $this->total;
        $this->players[] = $player;
    }

    public function selectWinner() {
        $ticket = rand(0, $this->total);

        foreach ($this->players as $player)
            if ($ticket >= $player->tickets[0] && $ticket <= $player->tickets[1])
                return $player;
        return false;
    }
}

class Player {
    public $name = '';
    public $value = 0;
    public $tickets = array(0, 0);

    function __construct($name, $value) {
        $this->name = $name;
        $this->value = $value;
    }
}

$ticketMaster = new TicketMaster();

$ticketMaster->addPlayer(new Player("John", 200));
$ticketMaster->addPlayer(new Player("Mike", 200));
$ticketMaster->addPlayer(new Player("Dave", 1000));

echo $ticketMaster->selectWinner()->name;

另外

$ticket = rand(0, $this->total); //change to random_int, but I kept it at rand because eval.in only works with this one

OutputDave

戴夫大部分时间都赢了,因为他有1000张门票,而其他两位只有400张门票的球员。

答案 2 :(得分:1)

- 不需要php,只需要mysql。在这个例子中,没有疯狂的行数只有12个。

create table rg
(   -- RaffleGuy
    guyId int auto_increment primary key,
    fullName varchar(100) not null,
    cellPhone varchar(20) not null,
    ticketCount int not null,
    winLow int null,
    winHigh int null
);

-- trucate table rg;    -- for testing next time
insert rg (fullName,cellPhone,ticketCount) values ('johnny two thumbs','11111',126);
insert rg (fullName,cellPhone,ticketCount) values ('kim','153111',500);
insert rg (fullName,cellPhone,ticketCount) values ('Lady with Hat','113211',1);
insert rg (fullName,cellPhone,ticketCount) values ('Guy with Nose','14454111',900);
insert rg (fullName,cellPhone,ticketCount) values ('Kipper','2211111',100);
insert rg (fullName,cellPhone,ticketCount) values ('Jipper','222888',400);
insert rg (fullName,cellPhone,ticketCount) values ('smith family','534511111',500);
insert rg (fullName,cellPhone,ticketCount) values ('First Pentacostal Church','3153111',200);
insert rg (fullName,cellPhone,ticketCount) values ('Lady with Hat','1132141',123);
insert rg (fullName,cellPhone,ticketCount) values ('Guy with Nose','14441311',500);
insert rg (fullName,cellPhone,ticketCount) values ('Kipper','2211711',300);
insert rg (fullName,cellPhone,ticketCount) values ('Jipper','2272',200);

update rg
join
(select rg.guyId,(select ifnull(sum(ticketCount)+1,1) from rg r2 where r2.guyId<rg.guyId) below
from rg) nnn
on nnn.guyId=rg.guyId
set winLow=nnn.below,winHigh=nnn.below+ticketCount-1

select * from rg;
#   fullName                    cell#       tix     wLow    wHigh   

1   johnny two thumbs           11111       126     1       126
2   kim                         153111      500     127     626
3   Lady with Hat               113211      1       627     627
4   Guy with Nose               14454111    900     628     1527
5   Kipper                      2211111     100     1528    1627
6   Jipper                      222888      400     1628    2027
7   smith family                534511111   500     2028    2527
8   First Pentacostal Church    3153111     200     2528    2727
9   Lady with Hat               1132141     123     2728    2850
10  Guy with Nose               14441311    500     2851    3350
11  Kipper                      2211711     300     3351    3650
12  Jipper                      2272        200     3651    3850


select sum(ticketCount) into @tottix from rg;   -- 3850

-- seed your random number, I leave that to you
select rand(unix_timestamp()); -- example

select floor(rand()*@tottix)+1 into @thernd; -- 531

select * from rg where winLow<=@thernd and winHigh>=@thernd;
    2   kim 153111  500 127 626

Kim wins, call her

答案 3 :(得分:0)

这是伪代码,可以帮助随机抽取彩票并确定获胜者。在使用foreach时,在这种情况下使用多个数字的最佳方法是使用数组。所以在下面的代码中

<?php
 //Array that holds the players
  $players[] =0;

 //Run this loop from 0 until how many tickets were issued
  for($i=0; $i<100; $i++){
      $players[$i] = $i * 100;
  }

 //Choosing the randomly winning ticket
  $winningTicket = array_rand($players, 1);

//what ticket number won the lottery, (var_dump to be able to see the output)
  echo "Winning Number = ".$players[$winningTicket]."<br/>";;

  //Who won the lottery, (var_dump to be able to see the output)
  echo "Winning Person = Person No.".$winningTicket;


?>