先透過觀察算試找出機率函式
n = 3
n = 5
n = 7
歸納出:
分子
為
分母
為
結論 :
計算
生日悖論在於如果站在自己的角度而言,要求有人跟我同一天生日的可能性(此為限定某日期)確實很低,需要至少 253 人在同一空間中才能保證有 50% 的發生機率。然而如果只要求有兩個人生日相同(沒有限定那一天)只需要 23 人就會有 50% 以上的發生機率(其實一樣是 magic number : 253 ,因為沒有特定要求日期因此只需要 23 個人就可以湊出這 253 種組合
參考資料:
The Birthday Attack
Birthday attack Wiki
現在的亂數產生器生成方式大多是透過數學公式來產生隨機的亂數,例如: Linear congruential generator 和 Middle-square method ,這類亂數產生器都需要輸入一個 seed 值來產生隨機的亂數序列,但是這邊的隨機其實是偽隨機,因為輸入同一個 seed 值會得到相同的序列以及這個亂數序列產生一定數量後就會循環。
而真正的隨機無法單單依靠數學算式來達成,但是可以透過觀察放射性物質在某一時間點的衰退速率或是利用環境噪音來做亂數的產生,因為這些是在現實生活中可以取得的真實隨機且無法再次重現,當然所需好廢的成本比較昂貴。
參考資料:
Random number generation
Cryptographically secure pseudorandom number generator
比較圖:
no | Macro | Function |
---|---|---|
1 | Macro is Preprocessed | Function is Compiled |
2 | No Type Checking | Type Checking is Done |
3 | Code Length Increases | Code Length remains Same |
4 | Use of macro can lead to side effect | No side Effect |
5 | Speed of Execution is Faster | Speed of Execution is Slower |
6 | Before Compilation macro name is replaced by macro value | During function call , Transfer of Control takes place |
7 | Useful where small code appears many time | Useful where large code appears many time |
8 | Generally Macros do not extend beyond one line | Function can be of any number of lines |
9 | Macro does not Check Compile Errors | Function Checks Compile Errors |
macro 和 function 十分相似但是又不同, macro 是直接用定義好的內容帶入到程式中做替換,由於 macro 沒有像 function 一樣需要 call 和 return 的程序,所以執行起來會比較快速。
參考資料:
Difference between macro and function in C Programming
What Differences are there Between Macros and Functions in C Language
1.linux/kernel/time/clocksource.c
為了避免 CPU 在熱插拔時發生死結(deadlock): clocksource mutex 和 cpu hotplug mutex ,延後 clock source 對 watchdog 的更新。
/**
* clocksource_mark_unstable - mark clocksource unstable via watchdog
* @cs: clocksource to be marked unstable
*
* This function is called instead of clocksource_change_rating from
* cpu hotplug code to avoid a deadlock between the clocksource mutex
* and the cpu hotplug mutex. It defers the update of the clocksource
* to the watchdog thread.
*/
void clocksource_mark_unstable(struct clocksource *cs)
{
unsigned long flags;
spin_lock_irqsave(&watchdog_lock, flags);
if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
if (list_empty(&cs->wd_list))
list_add(&cs->wd_list, &watchdog_list);
__clocksource_unstable(cs);
}
spin_unlock_irqrestore(&watchdog_lock, flags);
}
2
3.
typeof()
會回傳括號內變數的資料型態,在定義 macro 時是一個非常實用的 function
#define container_of(ptr, type, member) \
__extension__({ \
const __typeof__(((type *) 0)->member) *__pmember = (ptr); \
(type *) ((char *) __pmember - offsetof(type, member)); \
})
在 list.h
可以看到和這個 macro 相似的程式碼 :
/**
* container_of cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
list.h
還定義一系列操作,為什麼呢?這些有什麼益處?LIST_POISONING
這樣的設計有何意義?list_for_each_safe
和 list_for_each
的差異在哪?"safe" 在執行時期的影響為何?@
符號,這有何意義?你能否應用在後續的程式開發呢?tests/
目錄底下的 unit test 的作用為何?就軟體工程來說的精神為何?tests/
目錄的 unit test 可如何持續精進和改善呢?