Random.st
author claus
Fri, 16 Jul 1993 11:39:41 +0200
changeset 0 1cf8d1747859
child 2 07d9ee98e092
permissions -rw-r--r--
Initial revision

"======================================================================
|
| Copyright (C) 1988, 1989 Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 1, or (at your option) any later version.
| 
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
| details.
| 
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file LICENSE.  If not, write to the Free Software
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
|
 ======================================================================"


"
|     Change Log
| ============================================================================
| Author       Date       Change 
| claus       5 Apr 90    brought into Smalltalk/X
| sbyrne     19 Sep 89    Converted to use real method categories.
| sbyrne      3 Jul 89    created.
|
"

Stream subclass:#Random
       instanceVariableNames:'seed'
       classVariableNames:''
       poolDictionaries:''
       category:'Magnitude-Numbers'
!

Random comment:'

Copyright (C) 1988, 1989 Free Software Foundation, Inc.
Written by Steve Byrne.

random numbers - thanks to Steves GNU Smalltalk

@(#)Random.st   2.3 92/06/06
'!

!Random class methodsFor: 'instance creation'!

new
    ^self basicNew setSeed
! !

!Random methodsFor:'testing'!

chiSquare
    "returns on Sun3 93.40000000000009"
    ^self chiSquare: 1000 range: 100
!

chiSquare: n range: r
    | f t s |
    s := 1234567.
    f := Array new: r + 1.
    1 to: r + 1 do: [ :i | f at: i put: 0 ].
    n timesRepeat:
        [ s := (seed * 31415821) + 1 bitAnd: 16r3FFFFFFF.
          t := s \\ r.
          f at: t + 1 put: (f at: t + 1) + 1 ].
    t := 0.
    1 to: r do: [ :i | t := t + (f at: i) squared ].
    ^r asFloat * t / n - n

! !


!Random methodsFor: 'basic'!

atEnd
    ^false
!

next
    "From Sedgewick's 'Algorithms', based on Lehmer's method"

    "the times: is a kludge - times does not convert to LargeInteger on overflow"
    seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF.
    ^seed / 16r3FFFFFFF asFloat
!

nextBoolean
    "thanks to Peter Deutsch ..."
    seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF.
    ^ seed < 16r20000000
!

nextBetween:start and:stop
    |rnd|
    rnd := self next.
    rnd := rnd * (stop asFloat - start asFloat + 1.0).
    rnd := rnd + start asFloat.
    ^ rnd
!

nextPut: value
    self shouldNotImplement
!

nextMatchFor: aNumber
    ^self next = aNumber
! !


!Random methodsFor: 'private'!

setSeed
    seed := Time secondClock bitAnd: 16r3FFFFFFF
! !