Skip to content

Instantly share code, notes, and snippets.

View Generhr's full-sized avatar

Chad Blease Generhr

View GitHub Profile
@Generhr
Generhr / Ziggurat.ahk
Last active April 15, 2020 10:03
Ziggurat algorithm for Gaussian random number generation.
;* RandomNormal(Mean, Deviation)
;* Description:
;* https://en.wikipedia.org/wiki/Ziggurat_algorithm
RandomNormal(vMean := 0, vDeviation := 1.0) {
Static __K := (v := __RandomNormal()).k, __W := v.w, __F := v.f ;* Populate the lookup tables.
Loop {
u := RandomUniform(-0x80000000, 0x7FFFFFFF), i := (u & 0xFF) + 1
If (Abs(u) < __K[i]) { ;* Rectangle. This will be the case for 99.33% of values (512 rectangles would be 99.64%).
for i, v in [0x0200, 0x02A3, 0x02A1, 0x0201, 0x0202, 0x0203, 0x0204, 0x0205, 0x0206, 0x0207, 0x0208, 0x0209, 0x020A, 0x020E] {
OnMessage(v, "MsgHandler")
}
Exit
TrackMouseEvent(hWNDTrack, dwFlags := 0x00000002, dwHoverTime := 400) { ;: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-trackmouseevent
Static cbSize := 8 + (A_PtrSize*2)
VarSetCapacity(sEventTrack, cbSize, 0)
Class Timer {
Static Instances := []
__New(callback, interval := 0, priority := 0) {
Local
instance := {"Callback": callback, "Interval": interval, "Priority": priority
, "State": -1
, "Base": this.__Timer}
;* PostMessage(msg, (wParam), (lParam), (winTitle), (excludeTitle), (detectHiddenWindows))
PostMessage(msg, wParam := 0, lParam := 0, winTitle := "", excludeTitle := "", detectHiddenWindows := "") {
if (!(detectHiddenWindows == "") && !(detectHiddenWindows == (detect := A_DetectHiddenWindows))) {
DetectHiddenWindows, % detectHiddenWindows ;~ No error handling.
}
PostMessage, msg, wParam, lParam, , % winTitle, , % excludeTitle
if (detect) {
DetectHiddenWindows, % detect
mouseHook := SetWindowsHookEx(14, "LowLevelMouseProc")
, keyboardHook := SetWindowsHookEx(13, "LowLevelKeyboardProc")
exit
SetWindowsHookEx(idHook, callback) {
if (!hHook := DllCall(Format("SetWindowsHookEx{}", (A_IsUnicode) ? ("W") : ("A")), "Int", idHook, "Ptr", RegisterCallback(callback, "Fast"), "Ptr", DllCall("GetModuleHandle", "UInt", 0, "Ptr"), "UInt", 0, "Ptr")) {
throw (Exception(Format("0x{:X}", A_LastError), -1, FormatMessage(A_LastError)))
}