Hello, I’m trying to make a number of random number generators with fixed seeds and so far I came up with the following (define (make-seeded-rng seed)
(let ([old-rng (current-pseudo-random-generator)]
[new-rng (make-pseudo-random-generator)])
(current-pseudo-random-generator new-rng)
(random-seed seed)
(current-pseudo-random-generator old-rng)
new-rng))
Does anyone know of an easier way?
@mark.warren I think that’s about as good as it gets, but parameterize
takes care of some of the details there:
(define (make-seeded-rng seed)
(let ([new-rng (make-pseudo-random-generator)])
(parameterize ([current-pseudo-random-generator new-rng])
(random-seed seed))
new-rng))
@rokitna Thanks I wondered if I’d missed something. I’ve not used parameterize
before. Looks interesting.
They essentially manage this pattern for temporarily binding a value, and they do it in a way that cooperates well with first-class continuations.
A bunch of stuff in Racket has behavior that varies based on the current value of some parameters, so it’s pretty common to have to use parameterize
(or the pattern you were doing).
Cool thanks
@asumu has joined the channel
Whoops, didn’t see this ping until now because slack doesn’t ping me for channels I’m not in. @sydney.lambda I think what it comes down to is that the generic interface feature isn’t really intended to be used for full-on OO with inheritance. It’s more commonly used to, say, implement a uniform data structure interface with different structs. I think I would suggest trying to use the racket/class
library if you want to use inheritance to structure your code.
cheers @asumu
I think i need to learn how to make a macro to do what i want. wish me luck!