2013年2月14日 星期四

C++ 程式設計筆記 - 奇點亂數產生器[原創]

之前因為一些緣由需要設計一個亂數產生器,原本使用線性同餘法設計,線性同餘法的亂數產生器可由如下程式碼即可產生:

Seed = (Seed * Multiplier + Increment) % Modulos;

但測試後發現這樣產生的亂數有個非常大的問題就是產生的 Seed 如果曾經出現過那麼之後的亂數就會完全重複而產生循環,於是決定設計一個改良的亂數產生器,實作的程式碼如下:

GravitationalSingularityRandom.h :

class GravitationalSingularityRandom
{
private:
  long Seed;
  long Multiplier;
  long SourceIncrement;
  long Modulos;
  long TargetIndex;
  long TargetIncrement;
  unsigned char BufferRandom[256];
public:
  GravitationalSingularityRandom(long RandomSeed = 0);
  ~GravitationalSingularityRandom();
  void Random(unsigned char * Buffer, long Size);
};


GravitationalSingularityRandom.cpp :

#include "GravitationalSingularityRandom.h"

GravitationalSingularityRandom::GravitationalSingularityRandom(long RandomSeed)
{
  long i;
  Seed =  RandomSeed;
  Multiplier = 2593;
  SourceIncrement =  3079;
  TargetIncrement = 17;
  Modulos = 65537;
  TargetIndex = 0;
  for (i = 0; i < 256; i++)
  {
    BufferRandom[i] = (unsigned char) i;
  }
}

GravitationalSingularityRandom::~GravitationalSingularityRandom()
{
}

void GravitationalSingularityRandom::Random(unsigned char * Buffer, long BufferSize)
{
  int i;
  long SourceIndex = 0;
  unsigned char RandomMoving = 0;
  for (i = 0; i < BufferSize; i ++)
  {
    Seed = (Seed * Multiplier + SourceIncrement) % Modulos;
    SourceIndex = (Seed & 0xFF);
    TargetIndex = ((TargetIndex + TargetIncrement) & 0xFF);
    RandomMoving = BufferRandom[SourceIndex];
    BufferRandom[SourceIndex] = BufferRandom [TargetIndex];
    BufferRandom[TargetIndex] = RandomMoving;
    Buffer[i] = RandomMoving;
  }
}


一開始在建構式中產生一個大小為 0 - 255 的表格並在其中放入 0 - 255 的數字當作索引。程式碼中 Seed 、 Multiplier 、 SourceIncrement 跟 Modulos 都是用來產生線性同餘法的亂數所需的參數,但是卻不能將之直接拿來使用,而是拿來當表格的互換索引,也就是說線性同餘法所產生的亂數不是拿來直接使用而是用做此演算法所需的奇點,這點有點像是洗牌法但不同的是洗牌法是要產生連續不重複亂數,而這個作法是為了要避免亂數進入循環,並且產生的數值愈多接下來產生數值的複雜度也就愈高,經測試這個亂數產生器能產生非常接近自然亂數的數值。

2013年1月20日 星期日

運動健身筆記 - 運動健身以來的小小心得

前陣子在公司的游泳健身社月會做心得分享,為方便做心得的匯整,把內容節錄如下:

一、做重力訓練最大重點是訓練的頻率,做重力訓練時所做的事實上是破壞肌肉中的纖維組織,平時肌肉細胞因受纖維組織束袱,不易增長,故需做重力訓練對肌肉做相當的纖維組織破壞,並不是做重力訓練時肌肉會增長,而是重力訓練後大約一周內肌肉會增長,而且必需是在休息時肌肉才會增長,所以重力訓練後一段時間內不可再做重力相關訓練,一般建議頻率不可少於一週一次不可多於二天一次。

二、有氧訓練的頻率,一般建議一週三到五次,其原因為一週少於三次體力容易下降,多於五次運動效果與等於五次差不多,多於五次反而無法讓身體有充份休息的機會。

三、重力訓練器材的使用,以一遁環六下為原則,重力調整也以能支撐六下為原則,重力調整過高容易有運動傷害,過低效果不佳,如做完一遁環之後感覺該部位仍需加強再做一個遁環。

四、有氧訓練以一次三十分鐘為原則,三十分鐘的有氧訓練方能有效的提升心肺功能,並能分泌足量腦內啡達到舒解壓力的功效。

五、一般建議訓練順序為:跑步>重力>游泳,跑步能分泌大量腦內啡,如此可大幅降低重力訓練時的痛苦,而游泳可伸展重力訓練後的肌肉,有人提出游泳屬有氧運動會不會導致之前重力訓練的效果減少,這點的得分兩部份解釋,肌肉細胞變多變少還是肌肉細胞變大變小,重力訓練後一段時間內肌肉細胞會漸漸變多,但運動後因脂肪、醣類、蛋白質消耗,所以肌肉細胞快速變小,變多很慢變小很快,所以才有運動後肌肉先變小再變大的感覺,但變小的細胞補充營養後就會完全回復,所以游泳不會使重力效果降低。

六、模特兒的 BMI 參考值: 男模為 21 ~ 22 ,女模為 17 ~ 18。

2012年11月28日 星期三

Linux Mint 14 安裝筆記 - VAIO VPCYA16FW 無線網卡問題排除

在安裝 Linux Mint 14 後發現在 VAIO VPCYA16FW 之前開機捉不到無線網卡的問題已經被修復,安裝完已經可以正常捉到網路卡了,但仍有一個問題,當在 Panel 上的 Network Manager 關閉無線網卡的開關後會發現無線網卡再也不能開啟,從網路上的資料發現這個問題能用 sudo rfkill unblock wlan 這個命令解決,為了不用每次都下命令所以在 /usr/share/cinnamon/applets/network@cinnamon.org/applet.js 加入 + 號後的命令:

    setEnabled: function(enabled) {
        if (enabled) {
+          Util.spawnCommandLine("rfkill unblock wlan");
            this.statusItem.actor.show();
            this.section.actor.show();
        } else {
            this.statusItem.actor.hide();
            this.section.actor.hide();
        }
    },

2012年8月4日 星期六

Linux Mint 13 安裝筆記 - 嘸蝦米的 .deb 檔包裝

前陣子因為公司會查核盜版軟體所以想說自備正版輸入法這種每個人都不一樣公司不可能提供的軟體,因此可下載正版的 IBus 的嘸蝦米輸入法,不過行易所提供的是命令列的安裝方式使用上不太方便,因為這個緣故所以我決定了自製無蝦米的 .deb 檔。

1. 檢查並安裝這些套件。
           dpkg
           dpkg-deb
           dh-make
           debhelper
           devscripts
           fakeroot
           lintian

2. 建立 ~/ibus-boshiamy-1.0.0 資料夾 (命名需為name-version這樣的格式)。

3. 在 ~/ibus-boshiamy-1.0.0 資料夾中執行 dh_make 產生 ~/ibus-boshiamy-1.0.0/debian 資料夾。

4. ~/ibus-boshiamy-1.0.0/debian 資料夾中只留下底下這些檔案其餘刪除(有些由去除 .ex 副檔名產生)。
           control
           changlog
           rules
           preinst
           postinst
           prerm
           postrm

5. control 內容如下。
           Source: ibus-boshiamy
           Section: utils
           Priority: extra
           Maintainer: elvis <hero.elvis@gmail.com>
           Build-Depends: debhelper (>= 8.0.0)
           Standards-Version: 1.0.0
           #Vcs-Git: git://git.debian.org/collab-maint/ibus-boshiamy.git
           #Vcs-Browser: http://git.debian.org/?p=collab-maint/ibus-boshiamy.git;a=summary

           Package: ibus-boshiamy
           Architecture: any
           Depends: ibus (>= 1.0.0)
           Description: Boshiamy, Chinese Input Method For IBus.
           IBus is an Intelligent Input Bus. It is a new input framework for Linux OS. It  provides full featured and user friendly input method user interface. It also may help developers to develop input method easily.
           IBus-Boshiamyis a IBus based IM Engine for Traditional Chinese

6. changelog 內容如下。
           ibus-boshiamy (1.0.0) unstable; urgency=low

               * Initial Release.

           -- elvis <hero.elvis@gmail.com>  Sat, 04 Aug 2012 07:29:25 +0800

7.  postinst 內容如下。
           #!/bin/sh
           # postinst script for ibus-boshiamy
           #
           # see: dh_installdeb(1)

           set -e

           IBUSDAEMON=/usr/bin/ibus-daemon

           # summary of how this script can be called:
           #        * <postinst> `configure' <most-recently-configured-version>
           #        * <old-postinst> `abort-upgrade' <new version>
           #        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
           #          <new-version>
           #        * <postinst> `abort-remove'
           #        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
           #          <failed-install-package> <version> `removing'
           #          <conflicting-package> <version>
           # for details, see http://www.debian.org/doc/debian-policy/ or
           # the debian-policy package


           case "$1" in
               configure)
               $IBUSDAEMON -r -d
               ;;

               abort-upgrade|abort-remove|abort-deconfigure)
               ;;

               *)
                   echo "postinst called with unknown argument \`$1'" >&2
                   exit 1
               ;;
           esac

           # dh_installdeb will replace this with shell code automatically
           # generated by other debhelper scripts.

           #DEBHELPER#

           exit 0

8. postrm 內容如下。
           #!/bin/sh
           # postrm script for ibus-boshiamy
           #
           # see: dh_installdeb(1)

           set -e

           IBUSDAEMON=/usr/bin/ibus-daemon

           # summary of how this script can be called:
           #        * <postrm> `remove'
           #        * <postrm> `purge'
           #        * <old-postrm> `upgrade' <new-version>
           #        * <new-postrm> `failed-upgrade' <old-version>
           #        * <new-postrm> `abort-install'
           #        * <new-postrm> `abort-install' <old-version>
           #        * <new-postrm> `abort-upgrade' <old-version>
           #        * <disappearer's-postrm> `disappear' <overwriter>
           #          <overwriter-version>
           # for details, see http://www.debian.org/doc/debian-policy/ or
           # the debian-policy package


           case "$1" in
               purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
               $IBUSDAEMON -r -d
               ;;

               *)
                   echo "postrm called with unknown argument \`$1'" >&2
                   exit 1
               ;;
           esac

           # dh_installdeb will replace this with shell code automatically
           # generated by other debhelper scripts.

           #DEBHELPER#

           exit 0

9. 在 ~/ibus-boshiamy-1.0.0 執行 dpkg-buildpackage -rfakeroot 。

10. 建立 ~/boshiamy 資料夾並複製 ~/ibus-boshiamy-1.0.0/debian/temp/ 資料夾下的所有東西至 ~/boshiamy/ 底下。

11.  建立 ~/bushiamy/usr/share/ibus-table/tables 以及 ~/bushiamy/usr/share/ibus-table/icons 將行易的嘸蝦米 IBus 版底下的所有 .png 檔 複製至  ~/bushiamy/usr/share/ibus-table/icons/ 並且將行易的嘸蝦米 IBus 版底下的所有 .db 檔 複製至  ~/bushiamy/usr/share/ibus-table/tables/。

12. 在 ~/ 底下執行  dpkg -b boshiamy ibus-boshiamy_1.0.0_amd64.deb,boshiamy ibus-boshiamy_1.0.0_amd64.deb 即是所需要的安裝檔。



2011年12月27日 星期二

Linux Mint 12 安裝筆記 - Firefox 中文設定啟用

安裝完 Linux Mint 及中文語系之後打開Firefox介面選單仍為英文這是因為中文外掛模組未啟用到 Tools>Add-ons裏的 Languages 的設定中啟用 Traditional Chinese (zh-TW) Language Pack 8.0即可。





2011年12月24日 星期六

Linux Mint 12 安裝筆記 - VAIO VPCYA16FW 無線網卡問題排除

放入Linux Mint安裝光碟會發現無線網卡會無法Enable,經下如下指令:

sudo lshw -C network

檢查後發現系統多捉到了張Acer Wireless的卡,因此與VAIO的Atheros AR9285的驅動產生衝突,下lsmod會找到該多出來的無線網卡的模組名稱為acer_wmi,在安裝光碟打開命令列下如下指令:

sudo modprobe -r acer_wmi

後無線網卡運作就能正常,如此即可用無線網路安裝,安裝完後在/etc/modprobe.d/blacklist.conf的最後加上如下設定即可讓之後無線網卡都正常:

# Fix wireless issue for vaio
blacklist acer_wmi

Linux Mint 12 安裝筆記 - iBus 系統匣輸入法圖示顯示問題排除

裝好Linux Mint跟iBus 後發現切換輸入法時iBus的系統匣顯示的是禁止符號的圖示,我使用的解決方法是在 /usr/share/ibus/ui/gtk/panel.py這個檔案中加入下面+號後兩行的程式碼(因為是Python語法所以要注意程式碼前的空格數):

    def state_changed(self):
        if not self.__focus_ic:
            return

        enabled = self.__focus_ic.is_enabled()
        self.__language_bar.set_enabled(enabled)

        if enabled == False:
            self.reset()
            self.__set_im_icon(ICON_KEYBOARD)
            self.__set_im_name(None)
        else:
            engine = self.__focus_ic.get_engine()
            if engine:
+               pixbuf = gdk.pixbuf_new_from_file(engine.icon)
+               gtk.icon_theme_add_builtin_icon(engine.icon, 0, pixbuf)
                self.__set_im_icon(engine.icon)
                self.__set_im_name(engine.longname)
            else:
                self.__set_im_icon(ICON_KEYBOARD)
                self.__set_im_name(None)

這樣系統匣的輸入法圖示就可以正常顯示了