The Trunk: Kernel-ul.961.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

The Trunk: Kernel-ul.961.mcz

commits-2
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
  !


Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-ul.961.mcz

Ben Coman
On Tue, Oct 20, 2015 at 1:40 PM,  <[hidden email]> wrote:

> 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.

I'm curious what the use case for this is.
cheers -ben

Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-ul.961.mcz

Nicolas Cellier


2015-10-20 17:49 GMT+02:00 Ben Coman <[hidden email]>:
On Tue, Oct 20, 2015 at 1:40 PM,  <[hidden email]> wrote:
> 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.

I'm curious what the use case for this is.
cheers -ben


for example, generating UUID without the holly plugin...


Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-ul.961.mcz

Tobias Pape
My post to the inbox seem related here…

Best regards
        -Tobias

On 20.10.2015, at 17:54, Nicolas Cellier <[hidden email]> wrote:

>
>
> 2015-10-20 17:49 GMT+02:00 Ben Coman <[hidden email]>:
> On Tue, Oct 20, 2015 at 1:40 PM,  <[hidden email]> wrote:
> > 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.
>
> I'm curious what the use case for this is.
> cheers -ben
>
>
> for example, generating UUID without the holly plugin...



Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-ul.961.mcz

Levente Uzonyi-2
In reply to this post by Nicolas Cellier
That's the most actual use-case, but I've been thinking about creating
some primitives which would serve as an image-wide (CS)PRNG. Why?
1. primitives are faster; in case of RNG, they can be a lot faster (2-3
magnitudes)
2. there's no concurrency issue with primitives - all image-side code can
share the same PRNG.

Filling a ByteArray with random bytes would be one of the primitives.
Another one would be generating a random integer between two given
integers. And the third would be generating a random Float [0..1).

Levente

On Tue, 20 Oct 2015, Nicolas Cellier wrote:

>
>
> 2015-10-20 17:49 GMT+02:00 Ben Coman <[hidden email]>:
>       On Tue, Oct 20, 2015 at 1:40 PM,  <[hidden email]> wrote:
>       > 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.
>
>       I'm curious what the use case for this is.
>       cheers -ben
>
>
> for example, generating UUID without the holly plugin...
>
>