Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.lisp > #60784 > unrolled thread

Make a random-state from a non-random source

Started byStefan Monnier <monnier@iro.umontreal.ca>
First post2026-05-04 12:46 -0400
Last post2026-06-01 23:58 +0000
Articles 8 — 6 participants

Back to article view | Back to comp.lang.lisp


Contents

  Make a random-state from a non-random source Stefan Monnier <monnier@iro.umontreal.ca> - 2026-05-04 12:46 -0400
    Re: Make a random-state from a non-random source Alan Bawden <alan@csail.mit.edu> - 2026-05-05 00:45 -0400
      Re: Make a random-state from a non-random source Stefan Monnier <monnier@iro.umontreal.ca> - 2026-05-05 16:03 -0400
        Re: Make a random-state from a non-random source Kaz Kylheku <046-301-5902@kylheku.com> - 2026-06-04 23:45 +0000
    Re: Make a random-state from a non-random source tfb <no_email@invalid.invalid> - 2026-05-09 18:20 +0000
      Re: Make a random-state from a non-random source Stefan Monnier <monnier@iro.umontreal.ca> - 2026-05-11 11:23 -0400
    Re: Make a random-state from a non-random source steve g <sgonedes1977@gmail.com> - 2026-06-01 15:10 -0400
      Re: Make a random-state from a non-random source Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-06-01 23:58 +0000

#60784 — Make a random-state from a non-random source

FromStefan Monnier <monnier@iro.umontreal.ca>
Date2026-05-04 12:46 -0400
SubjectMake a random-state from a non-random source
Message-ID<jwvmryfku8t.fsf-monnier+comp.lang.lisp@gnu.org>
Reading the CLHS about `make-random-state` I can't see how to create
a "deterministic" random state.

I want it to be deterministic in the sense that if I re-run my program
(including after changing it a bit), I'll get the exact same sequence of
random numbers.
It's OK if the sequence is different when run on a different Lisp
implementation, OTOH.

I guess I could print a random state into a file and then read it back in,
but that depends on internals, so it's doubly messy.


=== Stefan

[toc] | [next] | [standalone]


#60785

FromAlan Bawden <alan@csail.mit.edu>
Date2026-05-05 00:45 -0400
Message-ID<86pl3a1n3a.fsf@williamsburg.bawden.org>
In reply to#60784
Stefan Monnier <monnier@iro.umontreal.ca> writes:

> Reading the CLHS about `make-random-state` I can't see how to create
> a "deterministic" random state.
>
> ...
>
> I guess I could print a random state into a file and then read it back in,
> but that depends on internals, so it's doubly messy.

That what you are intended to do.  That's why implementations are
required to provide a read syntax for objects of type RANDOM-STATE.
(See section 22.1.3.10 "Printing Random States" in the hyperspec.)

Yes, it could be more convenient, but it isn't.

- Alan

[toc] | [prev] | [next] | [standalone]


#60786

FromStefan Monnier <monnier@iro.umontreal.ca>
Date2026-05-05 16:03 -0400
Message-ID<jwvy0hxhbjr.fsf-monnier+comp.lang.lisp@gnu.org>
In reply to#60785
> That what you are intended to do.  That's why implementations are
> required to provide a read syntax for objects of type RANDOM-STATE.
> (See section 22.1.3.10 "Printing Random States" in the hyperspec.)
> Yes, it could be more convenient, but it isn't.

Is there some non-standard approach provided by some Common Lisp
implementations that is more convenient?

E.g. in ELisp, `random` can be passed a string as "random seed".


=== Stefan

[toc] | [prev] | [next] | [standalone]


#60806

FromKaz Kylheku <046-301-5902@kylheku.com>
Date2026-06-04 23:45 +0000
Message-ID<20260604163900.890@kylheku.com>
In reply to#60786
On 2026-05-05, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>> That what you are intended to do.  That's why implementations are
>> required to provide a read syntax for objects of type RANDOM-STATE.
>> (See section 22.1.3.10 "Printing Random States" in the hyperspec.)
>> Yes, it could be more convenient, but it isn't.
>
> Is there some non-standard approach provided by some Common Lisp
> implementations that is more convenient?
>
> E.g. in ELisp, `random` can be passed a string as "random seed".

From the CLHS documentation:

  Notes:

    One important use of make-random-state is to allow the same series
    of pseudo-random numbers to be generated many times within a single
    program.

But the way that languages/libraries typically support this "one
important use" is to make it easy to intialize a random state from
an integer, or array of integers/bytes or such.

:)

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

[toc] | [prev] | [next] | [standalone]


#60790

Fromtfb <no_email@invalid.invalid>
Date2026-05-09 18:20 +0000
Message-ID<10tntu1$3r6q9$1@dont-email.me>
In reply to#60784
Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> 
> I guess I could print a random state into a file and then read it back in,
> but that depends on internals, so it's doubly messy.
> 

It only depends on internals in the sense that you don't know what the
details of a random-state object are.  You *do* know that printing and then
reading one results in an equivalent object.

So all you need to do is (inside with-standard-io-syntax if need be) is
print one.  Then you can simply say

(defvar *my-random-state*
  #+<impl>
  <paste thing you just printed>
  ... versions for any other implementations ...
  #-(or <impl> ...)
  (error "add a case"))


-- 
www.tfeb.org/computer/

[toc] | [prev] | [next] | [standalone]


#60792

FromStefan Monnier <monnier@iro.umontreal.ca>
Date2026-05-11 11:23 -0400
Message-ID<jwvv7cu2cst.fsf-monnier+comp.lang.lisp@gnu.org>
In reply to#60790
tfb [2026-05-09 18:20:49] wrote:
> Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>> I guess I could print a random state into a file and then read it back in,
>> but that depends on internals, so it's doubly messy.
> It only depends on internals in the sense that you don't know what the
> details of a random-state object are.  You *do* know that printing and then
> reading one results in an equivalent object.
>
> So all you need to do is (inside with-standard-io-syntax if need be) is
> print one.  Then you can simply say
>
> (defvar *my-random-state*
>   #+<impl>
>   <paste thing you just printed>
>   ... versions for any other implementations ...
>   #-(or <impl> ...)
>   (error "add a case"))

Hmm... interesting.  So that a case where the availability of
reader-level conditionals has influenced the design of a "primitive".
Thanks,


=== Stefan

[toc] | [prev] | [next] | [standalone]


#60795

Fromsteve g <sgonedes1977@gmail.com>
Date2026-06-01 15:10 -0400
Message-ID<87h5nmule3.fsf@gmail.com>
In reply to#60784
Stefan Monnier <monnier@iro.umontreal.ca> writes:

> Reading the CLHS about `make-random-state` I can't see how to create
> a "deterministic" random state.
>
> I want it to be deterministic in the sense that if I re-run my program
> (including after changing it a bit), I'll get the exact same sequence of
> random numbers.
> It's OK if the sequence is different when run on a different Lisp
> implementation, OTOH.
>
> I guess I could print a random state into a file and then read it back in,
> but that depends on internals, so it's doubly messy.
>
>
> === Stefan


(defun gensym (&optional (thing "G"))
  "Creates a new uninterned symbol whose name is a prefix string (defaults
   to \"G\"), followed by a decimal number. Thing, when supplied, will
   alter the prefix if it is a string, or be used for the decimal number
   if it is a number, of this symbol. The default value of the number is
   the current value of *gensym-counter* which is incremented each time
   it is used."
  (multiple-value-bind (prefix int)
      (if (integerp thing)
          (values "G" thing)
          (values thing (let ((old *gensym-counter*))
                          (setq *gensym-counter* (1+ old))
                          old)))
    (make-symbol (%symbol-nameify prefix int))))




this is from sbcl symbol.lisp.

a very poor way of doing this would look like this...


(let ((*gensym-counter* 25))
           (gensym "CNT"))


I do not recommend doing this with gentemp, at least with sbcl.
Hencefold the worst way to make a random symol..!


[toc] | [prev] | [next] | [standalone]


#60797

FromLawrence D’Oliveiro <ldo@nz.invalid>
Date2026-06-01 23:58 +0000
Message-ID<10vl6bd$2j8gl$2@dont-email.me>
In reply to#60795
On Mon, 01 Jun 2026 15:10:44 -0400, steve g wrote:

> (defun gensym (&optional (thing "G"))
>   "Creates a new uninterned symbol whose name is a prefix string (defaults
>    to \"G\"), followed by a decimal number. Thing, when supplied, will
>    alter the prefix if it is a string, or be used for the decimal number
>    if it is a number, of this symbol. The default value of the number is
>    the current value of *gensym-counter* which is incremented each time
>    it is used."
>   (multiple-value-bind (prefix int)
>       (if (integerp thing)
>           (values "G" thing)
>           (values thing (let ((old *gensym-counter*))
>                           (setq *gensym-counter* (1+ old))
>                           old)))
>     (make-symbol (%symbol-nameify prefix int))))

    (defun gensym (&optional (thing "G"))
        "Creates a new uninterned symbol whose name is a prefix string (defaults
         to \"G\"), followed by a decimal number. Thing, when supplied, will
         alter the prefix if it is a string, or be used for the decimal number
         if it is a number, of this symbol. The default value of the number is
         the current value of *gensym-counter* which is incremented each time
         it is used."
        (multiple-value-bind (prefix int)
            (if (integerp thing)
                (values "G" thing)
                (values
                    thing
                    (let ((old *gensym-counter*))
                        (setq *gensym-counter* (1+ old))
                        old
                    ) ; let
                )
            ) ; if
            (make-symbol (%symbol-nameify prefix int))
        )
    ) ; gensym

Well, it’s a start, anyway ...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.lisp


csiph-web