Skip to content

Instantly share code, notes, and snippets.

@amutake
Created August 1, 2023 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amutake/8f65f969f6e162dead0d07339d20d6c5 to your computer and use it in GitHub Desktop.
Save amutake/8f65f969f6e162dead0d07339d20d6c5 to your computer and use it in GitHub Desktop.
LCGs.v
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