-----Original Message-----
From: Pharo-dev [mailto:[email protected]] On Behalf Of Ben
Coman
Sent: Wednesday, September 16, 2015 6:39 PM
To: Pharo Development List <[email protected]>
Subject: Re: [Pharo-dev] Pedantic on Random>>nextValue method name

On Wed, Sep 16, 2015 at 11:22 PM, Henrik Nergaard <[email protected]> wrote:
>> The method #nextValue actually calculates the next value the seed 
>> should be, the current value can be accessed by sending #seed.

>> I do agree that #nextValue might not be the clearest method name for 
>> its use; perhaps #nextSeed would be a better name?

>Yes. Thats better again.  Now its a private method, so there should be 
>problem changing it ;) ??anyone

Continuing on another Random topic related to..
https://pharo.fogbugz.com/default.asp?16577

I notice that Collection>>atRandom is protected by a mutex but
Collection>>atRandom: is not.  This makes me believe the protection is
for the Random generator and not for the actual picking of the element from
the collection.  So it seems that #atRandom holds the mutex too long - for
both random generation and element picking.
(>
Is there a specific use for the mutex in this case? I tried to use a shared
random without the mutex, and forking a few block closures, worked fine..

If the mutex could be removed we could just add a method in the class side
of Random like this:

globalGenerator
^Generator ifNil: [ Generator := Random new]

<) 

Thus I wonder if rather than copying the existing pattern of
Collection>>atRandom to GlobalSharedRandom>>generateFor:  it might be
better to make a more generic SharedRandom as follows...

(>
Maybe :P

I had a look at the Random Superclass in SciSmalltalk; here #next is used to
create and give the next random value. #seed for seed, and #peek for
checking what the next value would be without changing the current state. 
The peek method could be a good addition in Random -> 
        Random>>#Peak
           Self nextValue / m

I would keep the way the #nextValue method behaves since this gives the
option to "peek" at the next value the Ivar seed will have without changing
it (could be useful?), but the name could be improved to be more clear. 
Perhaps the method name #nextSeed might not be so good after all, it might
imply that the method creates a new seed, but what it actually does is to go
to the next state in the random generation? ( so a better name could be
#nextState ?), (perhaps the Ivar seed would also be better called state or
internalState? )
        
<)

"Old calls to #next changed to private #nextValue"

Random>>nextValue
    | lo hi aLoRHi answer |
    hi := (seed quo: q) asFloat.
    lo := seed - (hi * q).  " = seed rem: q"
    aLoRHi := (a * lo) - (r * hi).
    seed := (aLoRHi > 0.0)
         ifTrue:  [aLoRHi]
         ifFalse: [aLoRHi + m].
    ^ seed / m  "this line moved here from old #next"

Random>>next
     ^ self nextValue

Random>>next: anInteger into: anArray
    1 to: anInteger do: [:index | anArray at: index put: self nextValue].
    ^ anArray

Random>>nextInt: anInteger
    anInteger strictlyPositive ifFalse: [ self error: 'Range must be
positive' ].
    anInteger asFloat isInfinite
        ifTrue: [^(self nextValue asFraction * anInteger) truncated + 1].
    ^ (self nextValue * anInteger) truncated + 1


"SharedRandom protects all calls to private #nextValue"

Random subclass: SharedRandom
     instanceVariableNames: 'mutex'

SharedRandom>>initialize
     mutex := Semaphore forMutualExclusion.

SharedRandom>>next
    ^ mutex critical: [ self nextValue ]

SharedRandom>>next: anInteger into: anArray
    ^ mutex critical: [ super next: anInteger into: anArray ].

SharedRandom>>nextInt: anInteger
    ^ mutex critical: [ nextInt: anInteger ].


Then...

Object subclass: GlobalSharedRandom
    classVariables: 'sharedRandom'

GlobalSharedRandom class>>initialize
    sharedRandom := SharedRandom new.

GlobalSharedRandom class>>generator
    ^ sharedRandom


And finally...

Collection>>atRandom
    ^ self randomAt: GlobalSharedRandom generator. (> +1 <)


What do you think?
cheers, Ben

What do you think?
Best regards,
Henrik





--
View this message in context: 
http://forum.world.st/Pedantic-on-Random-nextValue-method-name-tp4850582p4850743.html
Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.

Reply via email to