[squeak-dev] The Inbox: Kernel-nice.754.mcz

commits at source.squeak.org commits at source.squeak.org
Mon Apr 22 23:36:28 UTC 2013


Nicolas Cellier uploaded a new version of Kernel to project The Inbox:
http://source.squeak.org/inbox/Kernel-nice.754.mcz

==================== Summary ====================

Name: Kernel-nice.754
Author: nice
Time: 23 April 2013, 1:35:44.22 am
UUID: 6525e6a7-e2f0-4b62-87e1-38915e225671
Ancestors: Kernel-fbs.753

Quick workaround to generate a large random with a poor generator.

Notably, anything above 53 bits will be biased.
Moreover, asking 309 decimal digits or more will overflow a double.

THIS DESERVES MORE THOUGHTS:
The workaround should better be handled in the generator itself...

=============== Diff against Kernel-fbs.753 ===============

Item was added:
+ ----- Method: LargePositiveInteger>>atRandom: (in category 'truncation and round off') -----
+ atRandom: aGenerator
+ 	"Answer a random integer from 1 to self picked from aGenerator."
+ 
+ 	| h chunk n k head res nextBytes |
+ 	(h := self highBit) <= 48
+ 		ifTrue: [^super atRandom: aGenerator].
+ 	chunk := 1 << 48.
+ 	res := self class new: self digitLength neg: false.
+ 	n := h - 1 // 48.
+ 	1 to: n do: [:i |
+ 		nextBytes := (aGenerator nextInt: chunk) - 1.
+ 		1 to: 6 do: [:j |
+ 			res digitAt: i - 1 * 6 + j put: (nextBytes digitAt: j)]].
+ 	
+ 	k :=  h - (n * 48).
+ 	chunk := 1 << k.
+ 	head := self >> (n * 48).
+ 	
+ 	[(nextBytes := (aGenerator nextInt: chunk) - 1) > head
+ 		or:
+ 			[1 to: nextBytes digitLength do: [:j |
+ 				res digitAt: n * 6 + j put: (nextBytes digitAt: j)]].
+ 			res >= self] whileTrue.
+ 
+ 	^res + 1!



More information about the Squeak-dev mailing list