home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / random.st < prev    next >
Text File  |  1992-02-15  |  3KB  |  120 lines

  1. "======================================================================
  2. |
  3. |   Random number Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         16 Mar 91      Class creation now separate statement.
  34. |
  35. | sbyrne     19 Sep 89      Converted to use real method categories.
  36. |
  37. | sbyrne      3 Jul 89      created.
  38. |
  39. "
  40.  
  41. Stream subclass: #Random
  42.        instanceVariableNames: 'seed'
  43.        classVariableNames: ''
  44.        poolDictionaries: ''
  45.        category: nil
  46. !
  47.  
  48. Random comment: "Here's a random comment :-)"
  49. 'My instances are generator streams that produce random numbers, which are 
  50. floating point values between 0 and 1.'!
  51.  
  52. !Random class methodsFor: 'instance creation'!
  53.  
  54. new
  55.     ^self basicNew setSeed
  56. !!
  57.  
  58.  
  59.  
  60. !Random methodsFor: 'testing'!
  61.  
  62. chiSquare
  63.     "returns on Sun3 93.40000000000009"
  64.     ^self chiSquare: 1000 range: 100
  65. !
  66.  
  67. chiSquare: n range: r
  68.     | f t seed |
  69.     seed _ 1234567.
  70.     f _ Array new: r + 1.
  71.     1 to: r + 1 do: [ :i | f at: i put: 0 ].
  72.     n timesRepeat:
  73.     [ seed _ (seed * 31415821) + 1 bitAnd: 16r3FFFFFFF.
  74.           t _ seed \\ r.
  75.       f at: t + 1 put: (f at: t + 1) + 1 ].
  76.     t _ 0.
  77.     1 to: r do: [ :i | t _ t + (f at: i) squared ].
  78.     ^r asFloat * t / n - n
  79.  
  80. !!
  81.  
  82.  
  83. !Random methodsFor: 'basic'!
  84.  
  85. atEnd
  86.     ^false
  87. !
  88.  
  89. next
  90.     | value |
  91.     "From Sedgewick's 'Algorithms', based on Lehmer's method"
  92.     seed _ (seed * 31415821) + 1 bitAnd: 16r3FFFFFFF.
  93.     ^seed / 16r3FFFFFFF.0
  94. !
  95.  
  96. nextPut: value
  97.     self shouldNotImplement
  98. !
  99.  
  100. next: anInteger
  101.     | collection |
  102.     collection _ OrderedCollection new.
  103.     anInteger timesRepeat: [ collection add: self next ]. 
  104.     ^collection
  105. !
  106.  
  107. nextMatchFor: aNumber
  108.     ^self next = aNumber
  109. !!
  110.  
  111.  
  112.  
  113. !Random methodsFor: 'private'!
  114.  
  115. setSeed
  116.     seed _ Time secondClock
  117. !!
  118.