This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Seeding PRNG

4 posts · 6327 views

How do I seed the Emacs random number generator w/ an arbitrary seed?

If I just call
(random)
it uses the same seed w/ each run.

I know that I can call
(random t)
first, to get a "random" seed..

But I want to seed the PRNG w/ my own, pre-determined seed...

For instance, make a call (set-random-seed n), but be able to change n, to be (set-random-seed 1) to get one pattern of "random" numbers, then call (set-random-seed 2) to get another different pattern of random numbers, etc.

Re: Seeding PRNG

Looking at the Emacs_23.3 source code I see:
  • src/fns.c, line_94, DEFUN ("random" ... - the "random" Lisp function, implemented in C
  • src/sysdep.c, line_2408, void seed_random ... - the C-function computing the seed
  • src/sysdep.c, line_2427, long get_random ... - the C-function computing the random number
The main problem here is that the C-variable holding the "seed" is managed by the standard library of the underlying C-compiler, so there is no built-in uniform way to set or read the "seed" variable across multiple platforms and multiple C-compilers from the Emacs Lisp level.

In other words: there is no accessible internal Emacs variable which could be set to a new seed. "seed_random" and "get_random" both call functions from the standard C-library, depending on the platform where the C-code of Emacs was compiled, but both functions have no direct access to the "seed" variable of the C-library.

I assume that one of the reasons for the somewhat strange (random t) solution to compute a new seed was that "t" is independent from any hardware limitations or number formats, so it is guaranteed to work with even the oldest C-compilers.

Sorry for the bad news,

- edgar

Re: Seeding PRNG

There are some LISP extensions on the Emacs wiki which might serve as a basis for generating your own predetermined random numbers. But otherwise I think you are out of luck.

Re: Seeding PRNG

PRNGs typically use some sort of internal state to generate a sequence of "random" numbers. The seed is merely used to set that state. The question then turns to how you set the seed. If you need highly random numbers (like you're working on crypto algorithms), you're better of not using the PRNG in Emacs at all. At that point, if you're on Linux/FreeBSD, you can use /dev/random or /dev/urandom, depending on how paranoid you are.

If your needs are more pedestrian (a card game or something), you can simply read the current time with CURRENT-TIME and use the numbers returned to seed the RNG. If you want to do this somewhat portably, use both the seconds and microseconds values as part of the seed. Microseconds will be less predictable, but some systems don't use it and it will be zero everywhere, leaving you with just seconds.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/