(ある時刻) > (現在時刻) - (指定時間)
わかりにくいのでPHPコードで書くと
$a = array( array('name' => 'hoge', 'win' => 10), array('name' => 'piyo', 'win' => 50), array('name' => 'fuga', 'win' => 40), ); $b = rand(1, 10 + 50 + 40); $begin = 1; foreach($a as $c){ if($begin <= $b && $b < $begin + $c['win']){ echo $c['name'] . 'が当選'; } $begin = $begin + $c['win']; }
日付等の連続区間が重複しているかどうかがどうかというのはよくあること 基準範囲をA、開始をs、終了をe、比較対象をBとすると
As <= Be AND Ae >= Bs
このような判定で真となる場合重複しているといえる。
範囲の関係性を考えると
の5パターンになる
↑の関係式で5パターンすべてが満たされてることがわかる
Fisher–Yates shuffle - Wikipedia, the free encyclopedia
配列をランダムに並び替えるやり方。 JavaScript で実装すると
var a = [1, 2, 3, 4, 5]; var length = a.length; for(var i = length - 1; i > 0; i--) { var swapIndex = Math.floor(Math.random() * (i + 1)); var tmp = a[i]; a[i] = a[swapIndex]; a[swapIndex] = tmp; }
Math.random() は 0 から 1未満 の範囲で乱数を発生させるので最初は 0 から 5 未満の値が出る。そこで Math.floor しているので、 0 から 4 の値を取り出している。この index の要素を配列の末尾、最初ならindex 4 のと交換する。
そして交換の範囲を狭めながら後ろから確定させていく。
定義通り JavaScript のコードで書くとこんな感じ
function getPage(total, limit){ // ゼロはゼロ if(total == 0){ return 0; } // あまりなし if((total % limit) == 0){ return (total / limit); } // あまりの分足す return (total / limit) + 1; }
手抜き
function getPage(total, limit){ return Math.ceil(total / limit); }
切り上げの関数を使えばよい。
PHP ならば
function getPage($total, $limit){ return ceil($total / $limit); }