class: Random
authorClaus Gittinger <cg@exept.de>
Wed, 01 Oct 2014 17:14:03 +0200
changeset 3383 836706a681b9
parent 3382 0c6e6bcca5fb
child 3384 da820ca60855
class: Random added: #randomSeed
Random.st
--- a/Random.st	Wed Oct 01 17:01:03 2014 +0200
+++ b/Random.st	Wed Oct 01 17:14:03 2014 +0200
@@ -257,6 +257,35 @@
     "Modified: 21.8.1997 / 18:08:56 / cg"
 ! !
 
+!Random class methodsFor:'seeding'!
+
+randomSeed
+    "return a number useful for seeding.
+     This takes the current processor's time, plus the processor's process id,
+     plus some value depending on the memory allocation state,
+     plus a random salt."
+
+    |newSeed|
+
+    RandomSalt isNil ifTrue:[
+        RandomSalt := 1.
+    ] ifFalse:[
+        RandomSalt := RandomSalt + 1.
+    ].
+    newSeed := RandomSalt + (Time microsecondClockValue bitXor:OperatingSystem getProcessId).
+    newSeed := newSeed bitXor:(ObjectMemory addressOf:(Object new)).
+    newSeed := newSeed bitXor:(ObjectMemory oldSpaceUsed).
+    [
+        newSeed := newSeed bitXor:(OperatingSystem getCPUCycleCount).
+    ] on:PrimitiveFailure do:[].
+
+    ^ newSeed.
+
+    "
+     self randomSeed bitAnd:16rFFFFFFFF  
+    "
+! !
+
 !Random class methodsFor:'testing'!
 
 bucketTest: randy
@@ -707,10 +736,10 @@
 !Random class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.51 2014-10-01 12:50:50 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.52 2014-10-01 15:14:03 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.51 2014-10-01 12:50:50 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.52 2014-10-01 15:14:03 cg Exp $'
 ! !