[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## [Help-smalltalk] help with random numbers?

**From**: |
Paolo Bonzini |

**Subject**: |
[Help-smalltalk] help with random numbers? |

**Date**: |
Sat, 31 Jul 2010 15:27:30 +0200 |

**User-agent**: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.10) Gecko/20100621 Fedora/3.0.5-1.fc13 Lightning/1.0b2pre Thunderbird/3.0.5 |

I profiled this simple program
Eval [
| n k r |
r := Random new.
n := 100000.
k := 500.
(1 to: n) select: [:x |
(r between: 1 and: n) <= k
ifTrue: [ n := n - 1. k := k - 1] ifFalse: [n := n - 1];
yourself ]
]

`and I noticed that it spends 75% of the time generating random numbers.
`` Bytecode-wise it's not bad (30 bytecodes per random number), but
``floating point causes a large number of garbage collections. Anybody
``wants to give a shot at reimplementing Random with a good, fast rng?
``Remember that on 32-bit machines SmallIntegers are only from -2^30 to
``2^30-1.
`
FWIW, here is a "more optimized" version of the above:
o := OrderedCollection new.
1 to: n do: [:x |
((r between: 0 and: (n := n - 1)) < k
ifTrue: [o add: x. k := k - 1]) notNil ]
Either program is a good benchmark for your rng.
Paolo

**[Help-smalltalk] help with random numbers?**,
*Paolo Bonzini* **<=**