unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* SRFI-27, deterministic random generator for floats
@ 2023-09-28 23:56 Zelphir Kaltstahl
  2023-09-29  0:18 ` Jeronimo Pellegrini
  0 siblings, 1 reply; 3+ messages in thread
From: Zelphir Kaltstahl @ 2023-09-28 23:56 UTC (permalink / raw)
  To: Guile User

Hello Guile Users!

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-float-gen)

ice-9/boot-9.scm:1685:16: In procedure raise-exception:
In procedure struct-vtable: Wrong type argument in position 1 (expecting struct): #<random-state 7fe21e75abc0>

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.

(random-float-gen 5)

;;; <stdin>:11:0: warning: possibly wrong number of arguments to `#<procedure 7f00ce6fb3e0 at srfi/srfi-27.scm:83:5 ()>'
ice-9/boot-9.scm:1685:16: In procedure raise-exception:
Wrong number of arguments to #<procedure 7f00ce6fb3e0 at srfi/srfi-27.scm:83:5 ()>

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.

(random-float-gen 0 5)

;;; <stdin>:11:0: warning: possibly wrong number of arguments to `#<procedure 7f00ce6fb3e0 at srfi/srfi-27.scm:83:5 ()>'
ice-9/boot-9.scm:1685:16: In procedure raise-exception:
Wrong number of arguments to #<procedure 7f00ce6fb3e0 at srfi/srfi-27.scm:83:5 ()>

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
~~~~

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

~~~~
(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))

(random-integer-gen 10)
~~~~

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

(This is using Guile installed via Guix using the following channels:

~~~~
(list (channel
         (name 'guix)
         (url"https://git.savannah.gnu.org/git/guix.git")
         (branch "master")
         (commit
           "772eaa69f31457aa19ca4dc4ce755c791d722054")
         (introduction
           (make-channel-introduction
             "9edb3f66fd807b096b48283debdcddccfea34bad"
             (openpgp-fingerprint
               "BBB0 2DDF 2CEA F6A8 0D1D  E643 A2A0 6DF2 A33A 54FA")))))

~~~~

)

Regards,
Zelphir

-- 
repositories:https://notabug.org/ZelphirKaltstahl


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: SRFI-27, deterministic random generator for floats
  2023-09-28 23:56 SRFI-27, deterministic random generator for floats Zelphir Kaltstahl
@ 2023-09-29  0:18 ` Jeronimo Pellegrini
  2023-09-29  9:19   ` Zelphir Kaltstahl
  0 siblings, 1 reply; 3+ messages in thread
From: Jeronimo Pellegrini @ 2023-09-29  0:18 UTC (permalink / raw)
  To: Zelphir Kaltstahl; +Cc: Guile User

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.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: SRFI-27, deterministic random generator for floats
  2023-09-29  0:18 ` Jeronimo Pellegrini
@ 2023-09-29  9:19   ` Zelphir Kaltstahl
  0 siblings, 0 replies; 3+ messages in thread
From: Zelphir Kaltstahl @ 2023-09-29  9:19 UTC (permalink / raw)
  To: Jeronimo Pellegrini; +Cc: Guile User

On 9/29/23 02:18, Jeronimo Pellegrini wrote:
> On 2023-09-28 20:56, Zelphir Kaltstahl wrote:
>> 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.

Ah, thank you Jeronimo!

I was under the impression that random-source-make-reals wants the random state >.<

This apparently work, thank you!

Regards,
Zelphir

-- 
repositories: https://notabug.org/ZelphirKaltstahl




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-09-29  9:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-28 23:56 SRFI-27, deterministic random generator for floats Zelphir Kaltstahl
2023-09-29  0:18 ` Jeronimo Pellegrini
2023-09-29  9:19   ` Zelphir Kaltstahl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).