RandomTT800.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jan 2001 14:32:45 +0100
changeset 949 7cf579b6a2d8
parent 946 3ffec3103f5c
child 998 5f66a2dd6b7d
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:libbasic2' }"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
Object subclass:#RandomTT800
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
	instanceVariableNames:'k x mag01'
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
	classVariableNames:'M N'
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	poolDictionaries:''
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	category:'Magnitude-Numbers'
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
RandomTT800 comment:'A pseudo-random number generator; see below for references.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
This generator is much slower than Squeak''s Random class.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
It automatically seeds itself based on the millisecond clock.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
Using the generator:
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
        randy := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
        randy seed: anInteger. "optional; never use zero"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
        aRandom := randy next.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
Example (InspectIt):
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
        | r |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
        r := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
        (1 to: 50) collect: [ :n | r next ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
===================================================================
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
Originally a C-program for TT800 : July 8th 1996 Version
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
by M. Matsumoto, email: matumoto@math.keio.ac.jp
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
Generates one pseudorandom number with double precision which is uniformly distributed on [0,1]-interval for each call.  One may choose any initial 25 seeds except all zeros.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
See: ACM Transactions on Modelling and Computer Simulation,
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
Vol. 4, No. 3, 1994, pages 254-266.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
ABSTRACT 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
The twisted GFSR generators proposed in a previous article have a defect in k-distribution for k larger than the order of recurrence. In this follow up article, we introduce and analyze a new TGFSR variant having better k-distribution property. We provide an efficient algorithm to obtain the order of equidistribution, together with a tight upper bound on the order. We discuss a method to search for generators attaining this bound, and we list some of these such generators. The upper bound turns out to be (sometimes far) less than the maximum order of equidistribution for a generator of that period length, but far more than that for a GFSR with a working are of the same size.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
Previous paper:
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
                
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
ACM Transactions on Modeling and Computer Simulation 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
Volume 2 , Issue 3 (1992) Pages 179-194 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
Twisted GFSR generators 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
Makoto Matsumoto, and Yoshiharu Kurita 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
ABSTRACT 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
The generalized feed back shift register (GFSR) algorithm suggested by Lewis and Payne is a widely used pseudorandom number generator, but has the following serious drawbacks: (1) an initialization scheme to assure higher order equidistribution is involved and is time consuming; (2) each bit of the
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
generated words constitutes an m-sequence based on a primitive trinomials, which shows poor randomness with respect to weight distribution; (3) a large working area is necessary; (4) the period of sequence is far shorter than the theoretical upper bound. This paper presents the twisted GFSR (TGFSR) algorithm, a slightly but essentially modified version of the GFSR, which solves all the above problems without loss of merit. Some practical TGFSR generators were implemented and passed strict empirical tests. These new generators are most suitable for simulation of a large distributive system, which requires a number of mutually independent pseudorandom number generators with compact size.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
'
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
!RandomTT800 class methodsFor:'documentation'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
949
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
    56
LICENSE
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
    PLEASE READ THE FOLLOWING NOTICE AND DISCLAIMER CAREFULLY
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
    BEFORE DOWNLOADING THIS SOFTWARE.  BY DOWNLOADING THIS SOFTWARE
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    YOU ARE AGREEING TO BE BOUND BY THESE TERMS.  IF YOU DO NOT AGREE
949
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
    61
    TO THE TERMS, DO NOT DOWNLOAD.
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
    This software (Software), provided by IBM Corporation, may
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
    contain, or be derived from, code provided by Apple Computer,
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
    Inc., and is provided subject to the provisions of the Apple
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    Computer, Inc. Software License for the SQUEAK Smalltalk System
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
    which can be viewed at http://www.squeak.org/license.html. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
    This Software is provided AS IS, and IBM Corporation
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
    DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING, WITHOUT
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
    LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
    PARTICULAR PURPOSE, AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
    IBM DOES NOT WARRANT THAT THE FUNCTIONS CONTAINED IN THE SOFTWARE
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
    WILL MEET THE USERS' REQUIREMENTS, THAT THE OPERATION OF THE
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
    SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT DEFECTS
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    IN THE SOFTWARE WILL BE CORRECTED.  UNDER NO CIRCUMSTANCES SHALL
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
    IBM CORPORATION BE LIABLE FOR INCIDENTAL, INDIRECT OR CONSEQUENTIAL
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
    DAMAGES ARISING FROM OR RELATING TO USE OF THIS SOFTWARE.  The
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
    user of this Software agrees to indemnify and hold IBM Corporation
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
    harmless from any and all damages, liabilities, costs and expenses
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
    (including attorney's fees) incurred by IBM Corporation as a
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    result of any claim, proceeding or judgment to the extent it
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    arises out of or is connected in any manner with the operation,
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    use, distribution or modification of the Software, or the combination
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
    of the Software or modified Software with other programs.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
TT800OriginalCVersion
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
" From: http://random.mat.sbg.ac.at/ftp/pub/data/tt800.c 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
/* A C-program for TT800 : July 8th 1996 Version */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
/* by M. Matsumoto, email: matumoto@math.keio.ac.jp */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
/* genrand() generate one pseudorandom number with double precision */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
/* which is uniformly distributed on [0,1]-interval */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
/* for each call.  One may choose any initial 25 seeds */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
/* except all zeros. */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
/* See: ACM Transactions on Modelling and Computer Simulation, */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
/* Vol. 4, No. 3, 1994, pages 254-266. */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
/* ABSTRACT 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
The twisted GFSR generators proposed in a previous article have a defect in k-distribution for k larger than the order of recurrence. In this follow up article, we introduce and analyze a new TGFSR variant having better k-distribution property. We provide an efficient algorithm to obtain the order of equidistribution, together with a tight upper bound on the order. We discuss a method to search for generators attaining this bound, and we list some of these such generators. The upper bound turns out to be (sometimes far) less than the maximum order of equidistribution for a generator of that period length, but far more than that for a GFSR with a working are of the same size.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
*/
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
#include <stdio.h>
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
#define N 25
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
#define M 7
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
double
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
genrand()
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
{
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    unsigned long y;
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
    static int k = 0;
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
    static unsigned long x[N]={ /* initial 25 seeds, change as you wish */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
	0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23,
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
	0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825,
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
	0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f,
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
	0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9,
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
	0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
    };
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
    static unsigned long mag01[2]={ 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
        0x0, 0x8ebfd028 /* this is magic vector `a', don't change */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
    };
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
    if (k==N) { /* generate N words at one time */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
      int kk;
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
      for (kk=0;kk<N-M;kk++) {
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
	x[kk] = x[kk+M] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
      }
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
      for (; kk<N;kk++) {
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
	x[kk] = x[kk+(M-N)] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
      }
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
      k=0;
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
    }
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
    y = x[k];
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
    y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
    y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
    y &= 0xffffffff; /* you may delete this line if word size = 32 */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
/* 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
   the following line was added by Makoto Matsumoto in the 1996 version
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
   to improve lower bit's corellation.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
   Delete this line to o use the code published in 1994.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
*/
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
    y ^= (y >> 16); /* added to the 1994 version */
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
    k++;
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
    return( (double) y / (unsigned long) 0xffffffff);
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
}
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
documentation
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
"
949
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   155
    NO WARRANTY
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   156
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   157
    Slightly adapted Squeak code for fileIn into ST/X.
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   158
    The original comment was:
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   159
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   160
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
    A pseudo-random number generator; see below for references.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
    This generator is much slower than Squeak's Random class.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
    It automatically seeds itself based on the millisecond clock.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
    Using the generator:
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
            randy := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
            randy seed: anInteger. 'optional; never use zero'
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
            aRandom := randy next.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
    Example (InspectIt):
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
            | r |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
            r := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
            (1 to: 50) collect: [ :n | r next ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
    ===================================================================
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
    Originally a C-program for TT800 : July 8th 1996 Version
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
    by M. Matsumoto, email: matumoto@math.keio.ac.jp
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
    Generates one pseudorandom number with double precision which is uniformly distributed 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
    on [0,1]-interval for each call.  One may choose any initial 25 seeds except all zeros.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
    See: ACM Transactions on Modelling and Computer Simulation,
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
    Vol. 4, No. 3, 1994, pages 254-266.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
    ABSTRACT 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
    The twisted GFSR generators proposed in a previous article have a defect in k-distribution for k larger 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
    than the order of recurrence. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
    In this follow up article, we introduce and analyze a new TGFSR variant having better k-distribution property. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
    We provide an efficient algorithm to obtain the order of equidistribution, together with a tight upper bound 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
    on the order. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
    We discuss a method to search for generators attaining this bound, and we list some of these such generators. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
    The upper bound turns out to be (sometimes far) less than the maximum order of equidistribution for a generator 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
    of that period length, but far more than that for a GFSR with a working are of the same size.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
    Previous paper:
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
    ACM Transactions on Modeling and Computer Simulation 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
    Volume 2 , Issue 3 (1992) Pages 179-194 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
    Twisted GFSR generators 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
    Makoto Matsumoto, and Yoshiharu Kurita 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
    ABSTRACT 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
    The generalized feed back shift register (GFSR) algorithm suggested by Lewis and Payne is a widely used pseudorandom number generator, but has the following serious drawbacks: (1) an initialization scheme to assure higher order equidistribution is involved and is time consuming; (2) each bit of the
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
    generated words constitutes an m-sequence based on a primitive trinomials, which shows poor randomness with respect to weight distribution; (3) a large working area is necessary; (4) the period of sequence is far shorter than the theoretical upper bound. This paper presents the twisted GFSR (TGFSR) algorithm, a slightly but essentially modified version of the GFSR, which solves all the above problems without loss of merit. Some practical TGFSR generators were implemented and passed strict empirical tests. These new generators are most suitable for simulation of a large distributive system, which requires a number of mutually independent pseudorandom number generators with compact size.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
examples
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
                                                                [exBegin]
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
    | r |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
    r := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
    (1 to: 50) collect: [ :n | r next ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
                                                                [exEnd]
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
!RandomTT800 class methodsFor:'initialization'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
initialSeeds
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
 	" initial 25 seeds, change as you wish. (there must be N seeds) "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
	^ self originalSeeds
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
originalSeeds
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
    " original initial 25 seeds. DO NOT CHANGE "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
    ^ #(    16r95F24DAB  16r0B685215  16rE76CCAE7  16rAF3EC239  16r715FAD23
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
            16r24A590AD  16r69E4B5EF  16rBF456141  16r96BC1B7B  16rA7BDF825
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
            16rC1DE75B7  16r8858A9C9  16r2DA87693  16rB657F9DD  16rFFDC8A9F
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
            16r8121DA71  16r8B823ECB  16r885D05F5  16r4E20CD47  16r5A9AD5D9
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
            16r512C0C03  16rEA857CCD  16r4CC1D30F  16r8891A8A1  16rA6B7AADB ) copy
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
!RandomTT800 class methodsFor:'instantiation'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
new
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
	^ super new
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
		initialize
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
!RandomTT800 class methodsFor:'testing'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
bucketTest1
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
    " Answers an array with 50 elements each of which should hold an integer near the value 1000, 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
      the closer the better. "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
    " RandomTT800 bucketTest1 inspect "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
    | a r s k |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
    s := 50.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
    a := Array new: s.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
    a atAllPut: 0.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
    r := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
    1 to: 50000 do: [ :n |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
            k := (r next * s) truncated + 1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
            a at: k put: (a at: k) + 1 ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
    ^ a
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
firstInteger: s
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
    " Answers an array with the first 's' raw integer elements generated by the RNG using the original seeds. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
      Intended for testing only. "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
    " RandomTT800 firstInteger: 50 "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
    | a r |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
    a := Array new: s.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
    a atAllPut: 0.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
    r := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
    " set the original initial 25 seeds. "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
    r setSeeds: self originalSeeds.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   281
    1 to: s do: [ :n |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
            a at: n put: r nextInteger ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
    ^ a
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
theItsCompletelyBrokenTest
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
    " Test to see if generator is answering what it should. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
      Assumes that the initial seeds are what is given in the original version. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
      If this fails something is badly wrong; do not use this generator."
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
    " RandomTT800 theItsCompletelyBrokenTest "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
    | okResult |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
    okResult := #(
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
            16rBCF1F45A 16rA26BF07E 16r14AEFF49 16r6777A14E 16r880C242F 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
            16rECEF7842 16r3BD9352 16r1DD55C94 16r7BC39C7 16r75E78DC2 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
            16rF0CF8478 16rE2886F41 16rB63AC1A9 16r57858A58 16r16169989 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
            16rD8602211 16r31818D3 16r30D51520 16r1C61F026 16rB58FA81
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
            16r51AF5CAC 16r609D3850 16r278BF184 16r50C1F860 16rEE6F61B4 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
            16r33C2A07E 16r55EE93B7 16r40BD28C3 16r713DB4BE 16rDD9352E3 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
            16r9254D8B9 16r9C02EE00 16r5F1BB40C 16rF741D0A5 16r6EE25C13 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
            16r375DD95B 16rFB24339 16rF3E2C95A 16r8CAA8C6F 16r63858F2F 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
            16r70369B29 16r617E2292 16r357EC977 16rC0B7E080 16r16474ADA 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
            16rAFDF1588 16rA1502F9D 16rC4577788 16rE3A9893C 16r71662621 ).
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
    (self firstInteger: 50) = okResult ifFalse: [
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
            PopUpMenu notify: 'First 50 results do not match the expected results.' ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
    (self firstInteger: 1001) last = 16rD7B4E10B ifFalse: [
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
            PopUpMenu notify: 'Element 1001 does not match the expected result.' ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
    PopUpMenu notify: 'The expected results were obtained.'
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
!RandomTT800 methodsFor:'operation'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
next
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
    " Answer the next random number as a float in the range [0.0,1.0) "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   319
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   320
    ^ self nextInteger asFloat / 4.29496729500000e9  "16rFFFFFFFF asFloat"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   321
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
    " Note that: 4.29496729500000e9 asTrueFraction hex 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
      is: '16rFFFFFFFF' "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
seed: anInteger
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
    x := self class initialSeeds.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
    1 to: x size do: [ :n |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   330
            x at: n put: ((x at: n) bitXor: anInteger) ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   331
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   332
    k := 0.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
!RandomTT800 methodsFor:'private'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
949
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   337
initialize
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   338
    N := 25.
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   339
    M := 7.
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   340
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   341
    k := 0.
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   342
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   343
    "x := self class initialSeeds.  (Done in #seed:) "
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   344
    self seed: Time millisecondClockValue.
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   345
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   346
    " this is magic vector `a', don't change "
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   347
    mag01 := #( 16r0 16r8EBFD028 ). 
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   348
!
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   349
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   350
nextInteger
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   351
    " Answer the next random number in its raw integer form "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   352
    | y kk jLast |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   353
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   354
    " generate N words at one time "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   355
    k = N ifTrue: [
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   356
            "for (kk=0;kk<N-M;kk++) {"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   357
            0 to: N-M-1 by: 1 do: [ :j |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   358
                    jLast := j.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   359
                    kk := j+1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   360
                    "x[kk] = x[kk+M] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   361
                    x at: kk put: 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
                            ( ((x at: kk+M) bitXor: (x at: kk) >> 1)
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
                                    bitXor: (mag01 at: (x at: kk) \\ 2 + 1) )
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   364
            ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
            "for (; kk<N;kk++) { "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
            jLast+1 to: N-1 by: 1 do: [ :j |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
                    kk := j+1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
                    "x[kk] = x[kk+(M-N)] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
                    x at: kk put:
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
                            ( ((x at: kk+M-N) bitXor: (x at: kk) >> 1)
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
                                    bitXor: (mag01 at: (x at: kk) \\ 2 + 1) )
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
                    ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
            k := 0.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
            ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
    y := x at: k+1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
    "y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
    y := y bitXor: ((y << 7) bitAnd: 16r2B5B2500). " s and b, magic vectors "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
    y := y bitAnd: 16rFFFFFFFF.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
    "y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
    y := y bitXor: ((y << 15) bitAnd: 16rDB8B0000). " t and c, magic vectors "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
    y := y bitAnd: 16rFFFFFFFF. " you may delete this line if word size = 32 "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
    "The following line was added by Makoto Matsumoto in the 1996 version
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
    to improve lower bit's corellation.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
    Delete this line to o use the code published in 1994. "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
    y := y bitXor: y >> 16. "added to the 1994 version"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   391
    k := k + 1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
    ^ y
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
setSeeds: anArray
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   396
    " Used only by class methods for testing. "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
    anArray size = x size ifTrue: [
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
            x := anArray ]
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
!RandomTT800 class methodsFor:'documentation'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
version
949
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   405
    ^ '$Header: /cvs/stx/stx/libbasic2/RandomTT800.st,v 1.2 2001-01-09 13:32:45 cg Exp $'
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
! !