Deciding when to use subclassing and when to use an instance variable is not always easy. An argument can be made that DiscreteProbability should really be a subclass of Random, such as the following:
Class DiscreteProbability :Random | weights max | defineWeights: anArray
weights $\leftarrow$ anArray.
max $\leftarrow$ anArray inject: 0 into: $[: x: y \mid x+y]$
next $[$ index value $\mid$
value $\leftarrow$ super randinteger: max.
index $\leftarrow 1$.
[value $>$ (weights at: index)]
whileTrue: [value $\leftarrow$ value - (weights at: index).
index $\leftarrow$ index +1$].$
$\uparrow$ index
defineWeights: anArray
weights $\leftarrow$ anArray.
max $\leftarrow$ anArray inject: 0 into: [:x:y|x+y]
next | index value |
value $\leftarrow$ super randinteger: max.
index $\leftarrow 1$.
[value $>$ (weights at: index)]
whileTrue: [value $\leftarrow$ value - (weights at: index).
index $\leftarrow$ index +1$]$.
$\uparrow$ index
]
Look at the class description for Random, in particular the response to the message randInteger:; and then describe why this class description will not produce the desired result.