Programming Praxis put out the previous had us composing already existing PRNGs).
The first function is the middle square PRNG. Basically, you start with a seed with an even number of digits and then repeatedly square it and return the middle digits.
Here I have a function that will take any suitable seed and generate a sequence of psuedorandom numbers from it:
; middle square PRNG: square the seed and take the middle digits
(define (make-middle-square n)
(let ([digits (inexact->exact (ceiling (log n 10)))])
(let ([res (mod (div (* n n)
(expt 10 (/ digits 2))) (expt 10 digits))])
(set! n res)
An example of running it (using the same seed from the Middle square):
~ (define middle-square (make-middle-square 675248))
The second example is perhaps even simpler. This time you start with any odd seed and apply the relation xn+1 = 65539 * xn mod 231:
; randu PRNG: x_n+1 = 65539 * x_n mod 2^31 (assume x_0 is odd)
(let ([2**31 (expt 2 31)])
(let ([res (mod (* 65539 x) 2**31)])
(set! x res)
An example run:
~ (define randu (make-randu 675249))
Pretty neat. Fortunately or not, this makes me want to implement a more heavyweight PRNG now, perhaps like the Mersenne Twister. We'll see.
If you'd like to download the full source code, you can do so here: two more random exercises source