[Haskell-beginners] IO ( stuff )
Isaac Dupree
ml at isaac.cedarswampstudios.org
Sat Dec 10 03:58:38 CET 2011
On 12/09/2011 05:27 PM, Brent Yorgey wrote:
> On Fri, Dec 09, 2011 at 05:05:15PM -0500, David McBride wrote:
>>
>> randomMonster :: RandomGen g => Rand g Monster
>> randomMonster = do
>> x<- getRandomR (0,2::Int)
>> return $ case x of
>> 0 -> Orc
>> 1 -> Dragon
>> 2 -> Wolf
>
> This looks like a bug to me. Everyone knows orcs are much more likely
> than dragons. ;)
I once wrote a function something like
randomDistributed :: (Num a, RandomGen g) => [(a, b)] -> g -> (b, g)
so that e.g.
randomDistributed [(10, Orc), (1, Dragon), (3.7, Wolf)]
would get you ten orcs for every dragon on average,
the overall chance of an orc being (10 / (10 + 1 + 3.7)).
~Isaac
More information about the Beginners
mailing list