2015年7月

php几个常用的概率算法(抽奖、广告首选)

做网站类的有时会弄个活动什么的,来让用户参加,既吸引用户注册,又提高网站的用户活跃度。同时参加的用户会获得一定的奖品,有100%中奖的,也有按一定概率中奖的,大的比如中个ipad、iphone5,小的中个Q币什么的。那么我们在程序里必然会设计到算法,即按照一定的概率让用户获得奖品。先来看两个概率算法函数。

 

算法一

/**
 * 全概率计算
 *
 * @param array $p array('a'=>0.5,'b'=>0.2,'c'=>0.4)
 * @return string 返回上面数组的key
 */
function random($ps){
    static $arr = array();
    $key = md5(serialize($ps));

    if (!isset($arr[$key])) {
        $max = array_sum($ps);
        foreach ($ps as $k=>$v) {
            $v = $v / $max * 10000;
            for ($i=0; $i<$v; $i++) $arr[$key][] = $k;
        }
    }
    return $arr[$key][mt_rand(0,count($arr[$key])-1)];
}

 

- 阅读剩余部分 -

Sublime Text2/3用户配置文件备份

关于将Subline Test打造的更漂亮的一些记录。

Package Control (Recommended)

If you have sublime package control installed, it's a breeze to install DefaultPlus.

If you don't have it installed, go here to learn how to install it. I really can't recommend it enough.

Open up the Tools->Command Pallette(⇧⌘P on OSX)

Type in install and select the Package Control: Install Package option.

Find Theme - DefaultPlusand hit Enter to install.

Git Install

You can install the theme and keep it updated by cloning the repo into your Packages directory.

To get to your packages directory, go into SublimeText and click on Preferences -> Browse Packages....

Now open up a terminal window and change your current directory to the the packages folder you opened

cd /path/to/packages/folder

Then clone the repository into the current directory under the name "Theme - DefaultPlus".

git clone https://github.com/mediachicken/sublimetext-defaultplus-theme "Theme - DefaultPlus"

Using the theme

Open up your settings Sublime Text -> Preferences -> Settings - User

Add (or change) the theme entry to "theme": "DefaultPlus Monokai.sublime-theme"

You can replace Monokai with any of the included themes. Make sure to change the color scheme too!

Theme Options

dplus_fat_tabs - If enabled, it will render large sized tabs.

dplus_bright_fonts - if enabled, it will use more legible fonts in the tabs/sidebar.

dplus_bright_minimap - if enabled, the minimap will be more vibrant in color.

dplus_bright_status_bar - if enabled, the bottom status bar will be vibrant in color.

- 阅读剩余部分 -

Linux解决使用root用户也无法删除文件

上次给服务器更换了系统,发现有个之前遗留下来的文件,居然在root权限下都无法删除,网上查了一下,原来需要这样做:

chattr -i xxx.ini
rm -f xxx.ini

i 属性专门用来保护重要的文件不被删除;