Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8329767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:54:17+00:00 2026-06-09T01:54:17+00:00

Similar: Haskell Random from Datatype I have created a data type to contain the

  • 0

Similar: Haskell Random from Datatype

I have created a data type to contain the different weapons in the Rock Paper Scissor game.

data Weapon = Rock | Paper | Scissor

Now I would like to generate a random weapon which will be used by the computer against the user.
I have take a look at the similar link I posted at the beginning but It seems too general for me.

I am able to generate random numbers from any other type. What I can get my head around is how to make my data type an instance of the Random class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:54:19+00:00Added an answer on June 9, 2026 at 1:54 am

    To generate a random Weapon, whether you make Weapon an instance of Random or not, what you need is a way to map numbers to Weapons. If you derive Enum for the type, a map to and from Ints is defined by the compiler. So you could define

    randomWeapon :: RandomGen g => g -> (Weapon, g)
    randomWeapon g = case randomR (0,2) g of
                       (r, g') -> (toEnum r, g')
    

    for example. With an Enum instance, you can also easily make Weapon an instance of Random:

    instance Random Weapon where
        random g = case randomR (0,2) g of
                     (r, g') -> (toEnum r, g')
        randomR (a,b) g = case randomR (fromEnum a, fromEnum b) g of
                            (r, g') -> (toEnum r, g')
    

    If there is a possibility of adding or removing constructors from the type, the best way to keep the bounds for randomR in sync with the type is to also derive Bounded, as Joachim Breitner immediately suggested:

    data Weapon
        = Rock
        | Paper
        | Scissors
          deriving (Bounded, Enum)
    
    instance Random Weapon where
        random g = case randomR (fromEnum (minBound :: Weapon), fromEnum (maxBound :: Weapon)) g of
                     (r, g') -> (toEnum r, g')
        randomR (a,b) g = case randomR (fromEnum a, fromEnum b) g of
                            (r, g') -> (toEnum r, g')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Haskell, I use the Data.Map module, and its principal type of the same
Is there any XML-(de)serializer for Haskell using Data / Typeable with functions similar to
Does Haskell have similar syntactic sugar to Python List Slices ? For instance in
I have two Haskell functions, both of which seem very similar to me. But
Is there a no-fuss serialization method for Haskell, similar to Erlang's term_to_binary/binary_to_term calls? Data.Binary
I'm new to Haskell. How come when I try to use Days from Data.Time
I am looking for a bisect operation in Haskell similar to Python's bisect_left() and
Similar questions to this my have been asked a lot of times before. But
Similar to the question Bitshift and integer promotion? , I have a question about
Similar to this question asking about relatively light-weight data entry / editor application development,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.