0
(define m (expt 2 32))
(define a 22695477)
(define c 1.0)

(define (integers-starting-from n)
    (stream-cons n (integers-starting-from (+ n 1))))

(define (prng seed)
  (define xn (remainder (+ c (* a seed)) m))
  (define prn (/ (remainder (+ c (* a seed)) m) m))
  (stream-cons prn
               (prng xn)))

When I run this code my current output is

(stream->list (prng 3) 5)
> (0.015852607786655426 0.4954120593611151 0.998752823099494 0.7253396362066269 0.03071586787700653)

But Output has to be

(stream->list (prng 3) 5)
> (0.01585 0.4954 0.9988 0.7253 0.0307)

How do I make output to ten-thousandth place value?

Eggcellentos
  • 1,570
  • 1
  • 18
  • 25
Honolulu
  • 23
  • 3

1 Answers1

1

Here's one way, if you're using Racket:

(define (prng seed)
  (define xn (remainder (+ c (* a seed)) m))
  (define prn (/ (remainder (+ c (* a seed)) m) m))
  (stream-cons (truncate prn 4)
               (prng xn)))

(define (truncate num precision)
  (string->number (~r num #:precision precision)))

Now the output will be:

'(0.0159 0.4954 0.9988 0.7253 0.0307)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • @Honolulu did this post solve your question? if so, please don’t forget to [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it, just click on the checkmark to its left ;) . – Óscar López Mar 05 '20 at 14:39