From c37c438348bb24831406ab22e5dc6e56b5d93203 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Wed, 6 Jul 2022 14:20:14 +0300 Subject: Implement RandomSource --- src/main.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 2c31bd9..c583dc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,12 +42,27 @@ struct NumberSource { impl Stream for NumberSource { type Item = u32; - fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { self.i += 1; Poll::Ready(Some(self.i)) } } +struct RandomSource { + seed: u32, + modulus: u32, + a: u32, + c: u32, +} + +impl Stream for RandomSource { + type Item = u32; + fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + self.seed = (self.a * self.seed + self.c) % self.modulus; + Poll::Ready(Some(self.seed)) + } +} + fn double>(input: S) -> impl Stream { stream! { for await val in input { -- cgit v1.3.1