Created
August 1, 2023 12:07
-
-
Save amutake/8f65f969f6e162dead0d07339d20d6c5 to your computer and use it in GitHub Desktop.
LCGs.v
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Require Import Coq.ZArith.ZArith. | |
Inductive rand : Set := | |
| State : Z -> rand. | |
(* 線形合同法による疑似乱数 *) | |
Definition next (r : rand) : rand := | |
match r with | |
| State s => | |
let s' := Z.modulo (Z.mul 48271 s) 2147483647 in | |
State s' | |
end. | |
Fixpoint nextN (n : nat) (r : rand) : rand := | |
match n with | |
| O => r | |
| S n => nextN n (next r) | |
end. | |
Definition get (r : rand) : Z := | |
match r with | |
| State s => s | |
end. | |
Require Import List. | |
Import ListNotations. | |
Fixpoint nexts (n : nat) (r : rand) : list Z := | |
match n with | |
| O => nil | |
| S n => get r :: nexts n (next r) | |
end. | |
Definition seed : Z := 1073741823. | |
Definition n := 12345. | |
Compute List.map (fun z => Z.modulo z 2) (nexts 10 (State seed)). | |
Compute Z.modulo (get (nextN n (State seed))) 2. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment