
I am trying to construct an infinate list of pairs of random colors. I am hung up on getting a random color. I have: data Color = Blue | Red | Green deriving (Eq, Ord, Show) am I supposed to instantiate a Random class instance from color? I am not sure exactly how the random number generator works. Is this the best way to go about it? Should I instead get a random Integer index of an array of Colors? thanks, -mike

G'day all. On Wed, Nov 20, 2002 at 08:44:36PM -0500, Mike T. Machenry wrote:
I am trying to construct an infinate list of pairs of random colors. I am hung up on getting a random color. I have:
data Color = Blue | Red | Green deriving (Eq, Ord, Show)
am I supposed to instantiate a Random class instance from color?
You could derive instances of Enum (and possibly also Bounded) and create random elements that way. Cheers, Andrew Bromage

Andrew and list, I am a beginer. I really don't know what I would do if I derived Color from Enum. You say I could create elements that way. Is there some simple example someone could post to the list? Thank you for your help. -mike On Thu, Nov 21, 2002 at 01:55:55PM +1100, Andrew J Bromage wrote:
G'day all.
On Wed, Nov 20, 2002 at 08:44:36PM -0500, Mike T. Machenry wrote:
I am trying to construct an infinate list of pairs of random colors. I am hung up on getting a random color. I have:
data Color = Blue | Red | Green deriving (Eq, Ord, Show)
am I supposed to instantiate a Random class instance from color?
You could derive instances of Enum (and possibly also Bounded) and create random elements that way.
Cheers, Andrew Bromage

"Mike T. Machenry" wrote:
Andrew and list,
I am a beginer. I really don't know what I would do if I derived Color from Enum. You say I could create elements that way. Is there some simple example someone could post to the list? Thank you for your help.
Here's one way to generate random values from an enumerable set: import Random randomR_Enum :: (Enum a, RandomGen g) => (a, a) -> g -> (a, g) randomR_Enum (lo, hi) gen = (toEnum val, gen') where (val, gen') = randomR (fromEnum lo, fromEnum hi) gen random_EnumBounded :: (Enum a, Bounded a, RandomGen g) => g -> (a, g) random_EnumBounded = randomR_Enum (minBound, maxBound) data Color = Blue | Red | Green deriving (Show, Enum, Bounded) instance Random Color where randomR = randomR_Enum random = random_EnumBounded main = do stdGen <- getStdGen print (take 10 (randoms stdGen :: [Color])) Dean

This program yeilds: Fail: toEnum{Color}: tag (4) is outside of enumeration's range (0,3) in ghc... any ideas why? -mike On Thu, Nov 21, 2002 at 10:07:12AM -0500, Dean Herington wrote:
"Mike T. Machenry" wrote:
Andrew and list,
I am a beginer. I really don't know what I would do if I derived Color from Enum. You say I could create elements that way. Is there some simple example someone could post to the list? Thank you for your help.
Here's one way to generate random values from an enumerable set:
import Random
randomR_Enum :: (Enum a, RandomGen g) => (a, a) -> g -> (a, g) randomR_Enum (lo, hi) gen = (toEnum val, gen') where (val, gen') = randomR (fromEnum lo, fromEnum hi) gen
random_EnumBounded :: (Enum a, Bounded a, RandomGen g) => g -> (a, g) random_EnumBounded = randomR_Enum (minBound, maxBound)
data Color = Blue | Red | Green deriving (Show, Enum, Bounded) instance Random Color where randomR = randomR_Enum random = random_EnumBounded
main = do stdGen <- getStdGen print (take 10 (randoms stdGen :: [Color]))
Dean

On Thu, 21 Nov 2002, Mike T. Machenry wrote:
This program yeilds: Fail: toEnum{Color}: tag (4) is outside of enumeration's range (0,3) in ghc... any ideas why?
-mike
Hmm. The program works fine for me, using GHC 5.04.1 or Hugs of Oct 2002. Dean
On Thu, Nov 21, 2002 at 10:07:12AM -0500, Dean Herington wrote:
"Mike T. Machenry" wrote:
Andrew and list,
I am a beginer. I really don't know what I would do if I derived Color from Enum. You say I could create elements that way. Is there some simple example someone could post to the list? Thank you for your help.
Here's one way to generate random values from an enumerable set:
import Random
randomR_Enum :: (Enum a, RandomGen g) => (a, a) -> g -> (a, g) randomR_Enum (lo, hi) gen = (toEnum val, gen') where (val, gen') = randomR (fromEnum lo, fromEnum hi) gen
random_EnumBounded :: (Enum a, Bounded a, RandomGen g) => g -> (a, g) random_EnumBounded = randomR_Enum (minBound, maxBound)
data Color = Blue | Red | Green deriving (Show, Enum, Bounded) instance Random Color where randomR = randomR_Enum random = random_EnumBounded
main = do stdGen <- getStdGen print (take 10 (randoms stdGen :: [Color]))
Dean
participants (3)
-
Andrew J Bromage
-
Dean Herington
-
Mike T. Machenry