[ Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters]

Arc de Triomphe clipart (2486 bytes)

TWISTER: A PRNG for SmallEiffel


Written by Arno Wagner.

Version 1.0

twister_1_0.zip (8,000 bytes) - source code
arno.wagner@acm.org Arno Wagner (maintainer) mail address.


A SmallEiffel library that implements the Mersenne Twister MT19937

Introduction

As I sometimes need a good PRNG, I have ported the Mersenne Twister MT19937 to SmallEiffel. This PRNG is really strong, as long as you don't use it directly for cryptography. It should be suitable for almost everything else. And, surprisingly, it is also very fast, even without using c-code!

Below I have atached the sources, which also include some speed comparisons and the URL of the developers of the algorithm in the header. More material on the Twister can be found there.

I hope some of you will find the Twister useful, and maybe it might even be useful enough to be included into future SmallEiffel releases :-)

As the members of this SmallEiffel list might remember, there was a discussion on PRNGs (Pseudo Random Number Generators) on this list some time ago. At that time there was no PRNG for SmallEiffel. By now we have MIN_STAND and STD_RAND which are somewhat simple, but more important we have GEN_RAND as interface.

As I need 32bit unsigned numbers for this, but didn't want to include c-code, I have used BIT 32. A class for unsigned 32-bit integers would be nice, but speed isn't too bad compared with MIN_STAND. STD_RAND is slower than MIN_STAND. Speed ( K6-2-300 with linux/egcs-2.91.66/SE-0.78 ):

Comparison

For comparison:

   original twister in c                 : 4 000 000 values/sec
   original twister in c            -O6  : 7 100 000 values/sec
   MIN_STAND, last_integer,              :   270 000 values/sec
   MIN_STAND, last_integer,         -O6  :   500 000 values/sec
   MIN_STAND, last_integer, -boost       : 2 000 000 values/sec
   MIN_STAND, last_integer, -boost  -O6  : 2 900 000 values/sec 

The twister in SmallEiffel:

   last_integer,                         :    50 000 values/sec
   last_integer,           -O6           :   120 000 values/sec
   last_integer,    -boost               :   870 000 values/sec
   last_integer,    -boost -O6           : 2 600 000 values/sec

   last_double:     ca.  80% of the performance of last_integer
   last_integer_mt: ca. 130% of the performance of last_integer

TWISTER: Short Form

class interface TWISTER

creation
   make
      --  Creation with default seed.

   with_seed (seed_value: INTEGER)
      --  Create the generator with an explicit seed_value.

feature(s) from GEN_RAND
   next
      --  Set last_value to new random value.

feature(s) from GEN_RAND
   --  No modifications :

   last_double: DOUBLE
      --  0 < Result < 1,
      ensure
         Result > 0 and Result < 1

   last_real: REAL
      --  0 < Result < 1
      ensure
         Result > 0 and Result < 1

   last_integer (n: INTEGER): INTEGER
      --   0 < Result < n+1
      require
         n >= 1
      ensure
         1 <= Result and Result <= n

feature(s) from TWISTER
   make
      --  Creation with default seed.

   with_seed (seed_value: INTEGER)
      --  Create the generator with an explicit seed_value.

feature(s) from TWISTER
   last_u32_mt: DOUBLE
      --  0 <= Result <= (2^32)-1, fractional part is 0
      --  result is DOUBLE because in this way all 32 bits
      --  can be stored. This feature is intended for
      --  validation against mt19337int.c

   last_integer_mt: INTEGER
      --  -2 ^(31) <= Result < 2^31


end of TWISTER

Further Information

Copyright (C) 1999 Arno Wagner

Based on code by Takuji Nishimura that was published under the GNU Library General Public License.

The code by Takuji Nishimura contained the following notice:

Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When you use this, send an e-mail to matumoto@math.keio.ac.jp with an appropriate reference to your work.

A CC to me (arno.wagner@acm.org) would be nice also.

Contact to M. Matsumoto and documentation/links for MT19937:

License

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Arno Wagner: arno.wagner@acm.org
Posted to the SmallEiffel Mailing List: http://www.egroups.com/group/smalleiffel/1170.html
26 November 1999 (Added to Eiffel Forum Archive: 23 December 1999)


[ Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters]