Skip to content

Instantly share code, notes, and snippets.

@chuntaro
Created August 17, 2018 22:48
Show Gist options
  • Select an option

  • Save chuntaro/a09f58a2e973cf48cda9e4c39845de12 to your computer and use it in GitHub Desktop.

Select an option

Save chuntaro/a09f58a2e973cf48cda9e4c39845de12 to your computer and use it in GitHub Desktop.
モンテカルロ法による円周率の計算 (ベンチマーク用)
;;; -*- lexical-binding: t; -*-
(defvar rand-next 1)
(defsubst myrand ()
"C 言語版や Common Lisp 版等と計算結果を合わせたい為に 32 bit の整形合同法を実装してます…"
(setq rand-next (logand (+ (logand (* rand-next 1103515245) #x7fffffff) 12345) #x7fffffff))
(logand (ash rand-next -16) #x7fff))
(defsubst float-rand ()
(/ (float (myrand)) 32768))
(defun calc-pi ()
(let ((count 0)
(max 10000000)
x y z)
(dotimes (_ max)
(setq x (float-rand)
y (float-rand)
z (+ (* x x) (* y y)))
(when (< z 1.0)
(setq count (1+ count))))
(* (/ (float count) max) 4)))
(let ((time (current-time))
my-pi)
(setq my-pi (calc-pi))
(print (float-time (time-since time)))
(print my-pi))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment