Seeding PRNG

Discussion of Emacs Lisp
Post Reply
Posts: 5
Joined: Sun May 01, 2011 1:41 pm

Seeding PRNG

Post by psholtz » Tue May 10, 2011 6:36 pm

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

If I just call

Code: Select all

it uses the same seed w/ each run.

I know that I can call

Code: Select all

(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.

Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Seeding PRNG

Post by edgar-rft » Thu May 12, 2011 8:17 am

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

Posts: 45
Joined: Tue Dec 14, 2010 1:39 am

Re: Seeding PRNG

Post by saulgoode » Thu May 12, 2011 12:20 pm

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.

Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX

Re: Seeding PRNG

Post by findinglisp » Sat Jun 04, 2011 7:48 am

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.

Post Reply