guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SRFI-27, deterministic random generator for floats


From: Jeronimo Pellegrini
Subject: Re: SRFI-27, deterministic random generator for floats
Date: Thu, 28 Sep 2023 21:18:28 -0300

On 2023-09-28 20:56, Zelphir Kaltstahl wrote:
Hello Guile Users!

Hello!

I am trying to deterministically generate random floats using SRFI-27 and I stumbled upon something weird:

~~~~
(use-modules (srfi srfi-27))

(define random-source (make-random-source))
(define random-state (random-source-pseudo-randomize! random-source 0 12345))
(define random-float-gen (random-source-make-reals random-state))

random-source-make-reals does not accept a random-state, but a random-source (which has an internal random-state, which you already randomized with random-source-pseudo-randomize!)

That's why Guile is complaining about a wrong type of argument -- a random-state struct was passed, but a random-source was expected.

(By the way, my interpretation of the SRFI is that random-source-pseudo-randomize! changes the internal state of the source passed to it, and it doesn't even need to return anything - Guile
seems to do it as a convenience)



I do not understand, what I am doing wrong, since the same seems to work for integers:

I do (see below). :)

~~~~
(use-modules (srfi srfi-27))

(define random-source (make-random-source))
(define random-state (random-source-pseudo-randomize! random-source 0 12345))
(define random-integer-gen (random-source-make-integers random-source))
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It works because you got it right this time (random-source, not random-state).

What do I need to do, to get a deterministic generator like I have for integers?

(define random-float-gen (random-source-make-reals random-source)) ;; <- source, not state

Does that fix it?

J.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]