[squeak-dev] The Trunk: Kernel-ul.961.mcz
commits at source.squeak.org
commits at source.squeak.org
Tue Oct 20 05:40:31 UTC 2015
Levente Uzonyi uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-ul.961.mcz
==================== Summary ====================
Name: Kernel-ul.961
Author: ul
Time: 20 October 2015, 7:39:47.9 am
UUID: ce0d735e-07a6-42e8-a789-40a8776796f4
Ancestors: Kernel-ul.960
Introduced Random >> #nextBytes:into:startingAt: which can be used to fill the indexable byte fields of an objects with random bytes efficiently. The method was extracted from #nextLargeInt:, which is its only user for now.
=============== Diff against Kernel-ul.960 ===============
Item was added:
+ ----- Method: Random>>nextBytes:into:startingAt: (in category 'accessing') -----
+ nextBytes: anInteger into: aBytesObject startingAt: startIndex
+ "Fill aBytesObject, an object with indexable byte fields, with anInteger number of random bytes starting from startIndex. Assume that MTw is at least 8."
+
+ | randomValue remainingBits index endIndex |
+ randomValue := remainingBits := 0.
+ index := startIndex.
+ endIndex := startIndex + anInteger - 1.
+ [ index <= endIndex ] whileTrue: [
+ remainingBits >= 8
+ ifTrue: [
+ aBytesObject basicAt: index put: (randomValue bitAnd: 16rFF).
+ randomValue := randomValue bitShift: -8.
+ remainingBits := remainingBits - 8.
+ index := index + 1 ]
+ ifFalse: [
+ remainingBits = 0
+ ifTrue: [ randomValue := self nextValue ]
+ ifFalse: [
+ | newRandomValue |
+ newRandomValue := self nextValue.
+ aBytesObject basicAt: index put: (randomValue bitShift: 8 - remainingBits) +
+ (newRandomValue bitAnd: (1 bitShift: 8 - remainingBits) - 1).
+ randomValue := newRandomValue bitShift: 0 - remainingBits.
+ index := index + 1 ].
+ remainingBits := MTw - remainingBits ] ]!
Item was changed:
----- Method: Random>>nextLargeInt: (in category 'accessing') -----
nextLargeInt: anInteger
"Answer a random integer value from the interval [1, anInteger]. This method works for arbitrarily large integers."
+ | byteCount bigRandom result firstDigit |
- | byteCount bigRandom remainder remainingBits i result firstDigit |
byteCount := anInteger digitLength + 4. "Extend the space with at least 32 bits for a fairer distribution."
bigRandom := LargePositiveInteger new: byteCount.
+ self nextBytes: byteCount into: bigRandom startingAt: 1.
- remainder := remainingBits := 0.
- i := 1.
- [ i <= byteCount ] whileTrue: [
- remainingBits >= 8
- ifTrue: [
- bigRandom digitAt: i put: (remainder bitAnd: 16rFF).
- remainder := remainder bitShift: -8.
- remainingBits := remainingBits - 8.
- i := i + 1 ]
- ifFalse: [
- remainingBits = 0
- ifTrue: [ remainder := self nextValue ]
- ifFalse: [
- | newRandom |
- newRandom := self nextValue.
- bigRandom digitAt: i put: (remainder bitShift: 8 - remainingBits) +
- (newRandom bitAnd: (1 bitShift: 8 - remainingBits) - 1).
- i := i + 1.
- remainder := newRandom bitShift: 0 - remainingBits ].
- remainingBits := MTw - remainingBits ] ].
result := anInteger * bigRandom bitShift: -8 * byteCount.
"Avoid using LargeInteger arithmetic for +1 in most cases."
result isLarge ifFalse: [ ^result + 1 ].
(firstDigit := result digitAt: 1) = 255 ifTrue: [ ^result + 1 ].
result digitAt: 1 put: firstDigit + 1.
^result
!
More information about the Squeak-dev
mailing list
|