RandomTT800.st
author Claus Gittinger <cg@exept.de>
Thu, 09 Jun 2016 17:01:18 +0200
changeset 3918 c565bdb0c8c4
parent 3415 ca707bcf23c8
child 4321 adfa19e74a4a
permissions -rw-r--r--
#OTHER by cg class: LinkedList changed: #at:ifAbsent:
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
998
5f66a2dd6b7d documentation
Claus Gittinger <cg@exept.de>
parents: 949
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
"
3352
6263fe92efbf comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents: 3350
diff changeset
   155
    Warning: this generator should not be used for cryptographic work.
949
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   156
3352
6263fe92efbf comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents: 3350
diff changeset
   157
    NO WARRANTY
3350
5e41f01f4dbb class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3337
diff changeset
   158
949
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   159
    Slightly adapted Squeak code for fileIn into ST/X.
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   160
    The original comment was:
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   161
7cf579b6a2d8 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 946
diff changeset
   162
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
    A pseudo-random number generator; see below for references.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
    This generator is much slower than Squeak's Random class.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
    It automatically seeds itself based on the millisecond clock.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
    Using the generator:
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
            randy := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
            randy seed: anInteger. 'optional; never use zero'
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
            aRandom := randy next.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
    Example (InspectIt):
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
            | r |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
            r := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
            (1 to: 50) collect: [ :n | r next ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
    ===================================================================
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
    Originally a C-program for TT800 : July 8th 1996 Version
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
    by M. Matsumoto, email: matumoto@math.keio.ac.jp
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
    Generates one pseudorandom number with double precision which is uniformly distributed 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
    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
   186
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
    See: ACM Transactions on Modelling and Computer Simulation,
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
    Vol. 4, No. 3, 1994, pages 254-266.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
    ABSTRACT 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
    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
   193
    than the order of recurrence. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
    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
   195
    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
   196
    on the order. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
    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
   198
    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
   199
    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
   200
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
    Previous paper:
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
    ACM Transactions on Modeling and Computer Simulation 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
    Volume 2 , Issue 3 (1992) Pages 179-194 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
    Twisted GFSR generators 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
    Makoto Matsumoto, and Yoshiharu Kurita 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
    ABSTRACT 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
    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
   211
    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.
998
5f66a2dd6b7d documentation
Claus Gittinger <cg@exept.de>
parents: 949
diff changeset
   212
5f66a2dd6b7d documentation
Claus Gittinger <cg@exept.de>
parents: 949
diff changeset
   213
    [see also:]
3392
2e2247b1dd41 documentation link
Claus Gittinger <cg@exept.de>
parents: 3385
diff changeset
   214
        http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
3398
dbd427ec32dd documentation link
Claus Gittinger <cg@exept.de>
parents: 3392
diff changeset
   215
        RandomGenerator - the default; uses the machine's /dev/random if available
998
5f66a2dd6b7d documentation
Claus Gittinger <cg@exept.de>
parents: 949
diff changeset
   216
        Random  - fast, but generates less quality random numbers
5f66a2dd6b7d documentation
Claus Gittinger <cg@exept.de>
parents: 949
diff changeset
   217
        RandomParkMiller - a good one (according to literature)
3392
2e2247b1dd41 documentation link
Claus Gittinger <cg@exept.de>
parents: 3385
diff changeset
   218
        RandomMT19973 - a better one (according to literature)
2e2247b1dd41 documentation link
Claus Gittinger <cg@exept.de>
parents: 3385
diff changeset
   219
        RandomKISS - a fast, reasonably good one (according to literature)
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
"
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
examples
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
                                                                [exBegin]
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
    | r |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
    r := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
    (1 to: 50) collect: [ :n | r next ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
                                                                [exEnd]
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
!RandomTT800 class methodsFor:'initialization'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
initialSeeds
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
 	" initial 25 seeds, change as you wish. (there must be N seeds) "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
	^ self originalSeeds
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
originalSeeds
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
    " original initial 25 seeds. DO NOT CHANGE "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
    ^ #(    16r95F24DAB  16r0B685215  16rE76CCAE7  16rAF3EC239  16r715FAD23
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
            16r24A590AD  16r69E4B5EF  16rBF456141  16r96BC1B7B  16rA7BDF825
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
            16rC1DE75B7  16r8858A9C9  16r2DA87693  16rB657F9DD  16rFFDC8A9F
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
            16r8121DA71  16r8B823ECB  16r885D05F5  16r4E20CD47  16r5A9AD5D9
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
            16r512C0C03  16rEA857CCD  16r4CC1D30F  16r8891A8A1  16rA6B7AADB ) copy
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
!RandomTT800 class methodsFor:'instantiation'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
new
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
	^ super new
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
		initialize
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
!RandomTT800 class methodsFor:'testing'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
bucketTest1
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
    " 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
   264
      the closer the better. "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
    " RandomTT800 bucketTest1 inspect "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
    | a r s k |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
    s := 50.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
    a := Array new: s.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
    a atAllPut: 0.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
    r := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
    1 to: 50000 do: [ :n |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
            k := (r next * s) truncated + 1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
            a at: k put: (a at: k) + 1 ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
    ^ a
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
firstInteger: s
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
    " 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
   281
      Intended for testing only. "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
    " RandomTT800 firstInteger: 50 "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
    | a r |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
    a := Array new: s.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
    a atAllPut: 0.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
    r := RandomTT800 new.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
    " set the original initial 25 seeds. "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
    r setSeeds: self originalSeeds.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
    1 to: s do: [ :n |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
            a at: n put: r nextInteger ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
    ^ a
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
theItsCompletelyBrokenTest
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
    " Test to see if generator is answering what it should. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
      Assumes that the initial seeds are what is given in the original version. 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
      If this fails something is badly wrong; do not use this generator."
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
    " RandomTT800 theItsCompletelyBrokenTest "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
    | okResult |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
    okResult := #(
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
            16rBCF1F45A 16rA26BF07E 16r14AEFF49 16r6777A14E 16r880C242F 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
            16rECEF7842 16r3BD9352 16r1DD55C94 16r7BC39C7 16r75E78DC2 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
            16rF0CF8478 16rE2886F41 16rB63AC1A9 16r57858A58 16r16169989 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
            16rD8602211 16r31818D3 16r30D51520 16r1C61F026 16rB58FA81
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
            16r51AF5CAC 16r609D3850 16r278BF184 16r50C1F860 16rEE6F61B4 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
            16r33C2A07E 16r55EE93B7 16r40BD28C3 16r713DB4BE 16rDD9352E3 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
            16r9254D8B9 16r9C02EE00 16r5F1BB40C 16rF741D0A5 16r6EE25C13 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
            16r375DD95B 16rFB24339 16rF3E2C95A 16r8CAA8C6F 16r63858F2F 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
            16r70369B29 16r617E2292 16r357EC977 16rC0B7E080 16r16474ADA 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
            16rAFDF1588 16rA1502F9D 16rC4577788 16rE3A9893C 16r71662621 ).
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
    (self firstInteger: 50) = okResult ifFalse: [
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
            PopUpMenu notify: 'First 50 results do not match the expected results.' ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   319
    (self firstInteger: 1001) last = 16rD7B4E10B ifFalse: [
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   320
            PopUpMenu notify: 'Element 1001 does not match the expected result.' ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   321
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
    PopUpMenu notify: 'The expected results were obtained.'
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
3415
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   325
!RandomTT800 methodsFor:'initialization'!
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   326
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   327
initialize
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   328
    N := 25.
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   329
    M := 7.
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   330
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   331
    k := 0.
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   332
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   333
    "x := self class initialSeeds.  (Done in #seed:) "
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   334
    self seed: (Random randomSeed bitAnd:16rFFFFFFFF).
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   335
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   336
    " this is magic vector `a', don't change "
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   337
    mag01 := #( 16r0 16r8EBFD028 ). 
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   338
!
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   339
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   340
seed: anInteger
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   341
    x := self class initialSeeds.
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   342
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   343
    1 to: x size do: [ :n |
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   344
            x at: n put: ((x at: n) bitXor: anInteger) ].
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   345
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   346
    k := 0.
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   347
!
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   348
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   349
setSeeds: anArray
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   350
    " Used only by class methods for testing. "
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   351
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   352
    anArray size = x size ifTrue: [
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   353
            x := anArray ]
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   354
! !
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   355
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   356
!RandomTT800 methodsFor:'random numbers'!
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   357
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   358
next
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   359
    " 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
   360
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   361
    ^ self nextInteger asFloat / 4.29496729500000e9  "16rFFFFFFFF asFloat"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
    " Note that: 4.29496729500000e9 asTrueFraction hex 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   364
      is: '16rFFFFFFFF' "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
3337
c4e5a66cad48 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   367
nextBoolean
c4e5a66cad48 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   368
    " Answer the next boolean"
c4e5a66cad48 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   369
c4e5a66cad48 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   370
    ^ self nextInteger > 16r7FFFFFFF
c4e5a66cad48 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   371
!
c4e5a66cad48 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   372
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
nextInteger
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
    " Answer the next random number in its raw integer form "
3415
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   375
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
    | y kk jLast |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
    " generate N words at one time "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
    k = N ifTrue: [
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
            "for (kk=0;kk<N-M;kk++) {"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
            0 to: N-M-1 by: 1 do: [ :j |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
                    jLast := j.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
                    kk := j+1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
                    "x[kk] = x[kk+M] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
                    x at: kk put: 
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
                            ( ((x at: kk+M) bitXor: (x at: kk) >> 1)
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
                                    bitXor: (mag01 at: (x at: kk) \\ 2 + 1) )
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
            ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
            "for (; kk<N;kk++) { "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
            jLast+1 to: N-1 by: 1 do: [ :j |
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   391
                    kk := j+1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
                    "x[kk] = x[kk+(M-N)] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2];"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
                    x at: kk put:
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
                            ( ((x at: kk+M-N) bitXor: (x at: kk) >> 1)
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
                                    bitXor: (mag01 at: (x at: kk) \\ 2 + 1) )
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   396
                    ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
            k := 0.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
            ].
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
    y := x at: k+1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
    "y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
    y := y bitXor: ((y << 7) bitAnd: 16r2B5B2500). " s and b, magic vectors "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
    y := y bitAnd: 16rFFFFFFFF.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
    "y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
    y := y bitXor: ((y << 15) bitAnd: 16rDB8B0000). " t and c, magic vectors "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
    y := y bitAnd: 16rFFFFFFFF. " you may delete this line if word size = 32 "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   409
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   410
    "The following line was added by Makoto Matsumoto in the 1996 version
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   411
    to improve lower bit's corellation.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   412
    Delete this line to o use the code published in 1994. "
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   413
    y := y bitXor: y >> 16. "added to the 1994 version"
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   414
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   415
    k := k + 1.
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
    ^ y
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   417
! !
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
!RandomTT800 class methodsFor:'documentation'!
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
version
3415
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   422
    ^ '$Header: /cvs/stx/stx/libbasic2/RandomTT800.st,v 1.10 2014-10-02 16:22:58 cg Exp $'
3350
5e41f01f4dbb class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3337
diff changeset
   423
!
5e41f01f4dbb class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3337
diff changeset
   424
5e41f01f4dbb class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3337
diff changeset
   425
version_CVS
3415
ca707bcf23c8 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 3398
diff changeset
   426
    ^ '$Header: /cvs/stx/stx/libbasic2/RandomTT800.st,v 1.10 2014-10-02 16:22:58 cg Exp $'
946
3ffec3103f5c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   427
! !
3337
c4e5a66cad48 class: RandomTT800
Claus Gittinger <cg@exept.de>
parents: 998
diff changeset
   428