本文目錄一覽:
php取隨機數概率算法
問題有點模糊,我先暫時把“保留兩位小數”理解成“[輸出的隨機數]保留兩位小數”,而非其他因素保留多少小數。
又把“1-10”,理解成包括1和10在內其兩數之間的小數,
把10-50,理解成10.01到50.00之間的數,不包括10.00。以此類推。
?php
function genRandom(){
$p=rand(1,100);
if($p=60) $r=rand(100,1000);
elseif($p60 and $p=85) $r=rand(1001,5000);
elseif($p85 and $p=95) $r=rand(5001,10000);
else $r=rand(10001, 20000);
return $r/100;
}
$a=array();
$total=50000;
for($i=0;$i$total;$i++){
$c=genRandom();
if($c=1 and $c=10) $p=0;
elseif($c10 and $c=50) $p=1;
elseif($c50 and $c=100) $p=2;
elseif($c100 and $c=200) $p=3;
else $p=4;
if(!array_key_exists($p, $a)) $a[$p]=1;
else $a[$p]++;
}
if(!array_key_exists(4, $a)) $a[4]=0;
echo “總樣本數”.$total.’br/’;
echo “1-10樣本數”.$a[0].’, 占’.($a[0]/$total*100).’%br/’;
echo “10-50樣本數”.$a[1].’, 占’.($a[1]/$total*100).’%br/’;
echo “50-100樣本數”.$a[2].’, 占’.($a[2]/$total*100).’%br/’;
echo “100-200樣本數”.$a[3].’, 占’.($a[3]/$total*100).’%br/’;
echo “其他樣本數”.$a[4].’, 占’.($a[4]/$total*100).’%br/’;
總樣本數50000
1-10樣本數30052, 佔60.104%
10-50樣本數12404, 佔24.808%
50-100樣本數4993, 佔9.986%
100-200樣本數2551, 佔5.102%
其他樣本數0, 佔0%
php幾種排序算法實例詳解
四種排序算法的PHP實現:
1) 插入排序(Insertion Sort)的基本思想是:
每次將一個待排序的記錄,按其關鍵字大小插入到前面已經排好序的子文件中的適當位置,直到全部記錄插入完成為止。
2) 選擇排序(Selection Sort)的基本思想是:
每一趟從待排序的記錄中選出關鍵字最小的記錄,順序放在已排好序的子文件的最後,直到全部記錄排序完畢。
3) 冒泡排序的基本思想是:
兩兩比較待排序記錄的關鍵字,發現兩個記錄的次序相反時即進行交換,直到沒有反序的記錄為止。
4) 快速排序實質上和冒泡排序一樣,都是屬於交換排序的一種應用。所以基本思想和上面的冒泡排序是一樣的。
1. sort.php文件如下:
?php
class Sort {
private $arr = array();
private $sort = ‘insert’;
private $marker = ‘_sort’;
private $debug = TRUE;
/**
* 構造函數
*
* @param array 例如:
$config = array (
‘arr’ = array(22,3,41,18) , //需要排序的數組值
‘sort’ = ‘insert’, //可能值: insert, select, bubble, quick
‘debug’ = TRUE //可能值: TRUE, FALSE
)
*/
public function construct($config = array()) {
if ( count($config) 0) {
$this-_init($config);
}
}
/**
* 獲取排序結果
*/
public function display() {
return $this-arr;
}
/**
* 初始化
*
* @param array
* @return bool
*/
private function _init($config = array()) {
//參數判斷
if ( !is_array($config) OR count($config) == 0) {
if ($this-debug === TRUE) {
$this-_log(“sort_init_param_invaild”);
}
return FALSE;
}
//初始化成員變量
foreach ($config as $key = $val) {
if ( isset($this-$key)) {
$this-$key = $val;
}
}
//調用相應的成員方法完成排序
$method = $this-sort . $this-marker;
if ( ! method_exists($this, $method)) {
if ($this-debug === TRUE) {
$this-_log(“sort_method_invaild”);
}
return FALSE;
}
if ( FALSE === ($this-arr = $this-$method($this-arr)))
return FALSE;
return TRUE;
}
/**
* 插入排序
*
* @param array
* @return bool
*/
private function insert_sort($arr) {
//參數判斷
if ( ! is_array($arr) OR count($arr) == 0) {
if ($this-debug === TRUE) {
$this-_log(“sort_array(insert)_invaild”);
}
return FALSE;
}
//具體實現
$count = count($arr);
for ($i = 1; $i $count; $i++) {
$tmp = $arr[$i];
for($j = $i-1; $j = 0; $j–) {
if($arr[$j] $tmp) {
$arr[$j+1] = $arr[$j];
$arr[$j] = $tmp;
}
}
}
return $arr;
}
/**
* 選擇排序
*
* @param array
* @return bool
*/
private function select_sort($arr) {
//參數判斷
if ( ! is_array($arr) OR count($arr) == 0) {
if ($this-debug === TRUE) {
$this-_log(“sort_array(select)_invaild”);
}
return FALSE;
}
//具體實現
$count = count($arr);
for ($i = 0; $i $count-1; $i++) {
$min = $i;
for ($j = $i+1; $j $count; $j++) {
if ($arr[$min] $arr[$j]) $min = $j;
}
if ($min != $i) {
$tmp = $arr[$min];
$arr[$min] = $arr[$i];
$arr[$i] = $tmp;
}
}
return $arr;
}
/**
* 冒泡排序
*
* @param array
* @return bool
*/
private function bubble_sort($arr) {
//參數判斷
if ( ! is_array($arr) OR count($arr) == 0) {
if ($this-debug === TRUE) {
$this-_log(“sort_array(bubble)_invaild”);
}
return FALSE;
}
//具體實現
$count = count($arr);
for ($i = 0; $i $count; $i++) {
for ($j = $count-1; $j $i; $j–) {
if ($arr[$j] $arr[$j-1]) {
$tmp = $arr[$j];
$arr[$j] = $arr[$j-1];
$arr[$j-1] = $tmp;
}
}
}
return $arr;
}
/**
* 快速排序
* @by
* @param array
* @return bool
*/
private function quick_sort($arr) {
//具體實現
if (count($arr) = 1) return $arr;
$key = $arr[0];
$left_arr = array();
$right_arr = array();
for ($i = 1; $i count($arr); $i++){
if ($arr[$i] = $key)
$left_arr[] = $arr[$i];
else
$right_arr[] = $arr[$i];
}
$left_arr = $this-quick_sort($left_arr);
$right_arr = $this-quick_sort($right_arr);
return array_merge($left_arr, array($key), $right_arr);
}
/**
* 日誌記錄
*/
private function _log($msg) {
$msg = ‘date[‘ . date(‘Y-m-d H:i:s’) . ‘] ‘ . $msg . ‘\n’;
return @file_put_contents(‘sort_err.log’, $msg, FILE_APPEND);
}
}
/*End of file sort.php*/
/*Location htdocs/sort.php */
2. sort_demo.php文件如下:
?php
require_once(‘sort.php’);
$config = array (
‘arr’ = array(23, 22, 41, 18, 20, 12, 200303,2200,1192) ,
//需要排序的數組值
‘sort’ = ‘select’,
//可能值: insert, select, bubble, quick
‘debug’ = TRUE
//可能值: TRUE, FALSE
);
$sort = new Sort($config);
//var_dump($config[‘arr’]);
var_dump($sort-display());
/*End of php*/
如何用PHP製作有獎品(數量)的轉盤抽獎?
這個不是有key值么,抽到後把這個key值的給unset()
unset() 方法
注意如果你使用 unset() 方法,它是不會改變其他的鍵(key),如果你想對其他的鍵(key)重新整理排序,可以使用 array_values()。
?php
$array = array(0 = “a”, 1 = “b”, 2 = “c”);unset($array[1]);
//↑ 你要刪除的數組元素值的鍵print_r($array);?
輸出結果:
Array (
[0] = a [2] = c)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/282800.html