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 6251267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:34:59+00:00 2026-05-24T13:34:59+00:00

Let’s say I want to generate a random number in Haskell. To do so,

  • 0

Let’s say I want to generate a random number in Haskell. To do so, I’ll make use of

randomRIO (0, 10)

that generates a number between 0 and 10 for me. It’s type is

randomRIO (0,10) :: (Random a, Num a) => IO a

Now, let’s say I assign its result to a value k. It’s type is IO Integer.

Trying to do usual arithmetic stuff such as k + 2 will yield the following result in Ghci:

<interactive>:1:3:
    No instance for (Num (IO Integer))
      arising from the literal `2'
    Possible fix: add an instance declaration for (Num (IO Integer))
    In the second argument of `(+)', namely `2'
    In the expression: k + 2
    In an equation for `it': it = k + 2

The problem also happens when trying asking Ghci about

[randomRIO (0, 10) | x <- [0..10]]

The error message is a bit cryptic, how can I make use of random numbers in Haskell?

  • 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-05-24T13:35:00+00:00Added an answer on May 24, 2026 at 1:35 pm

    A value of type IO Integer is not an integer. It’s an action that, when performed, will return an integer. The distinction is important. The only way to perform IO actions is to hook them up to main, or type them into GHCi.

    So randomRIO (0, 10) is an action, that when performed, returns a random number between 0 and 10. Note that this is not a function, as a function must always return the same result given the same input, although we sometimes call this an impure function.

    So the question is, how do you take an action returning an integer and make an action returning an integer plus two? Easy, you can use fmap to combine an action and a pure function transforming its result into a new action.

    fmap (+2) $ randomRIO (0, 10)
    

    Control.Monad contains many useful functions for building new actions out of other actions. For your second example, we can use replicateM, which creates an action that, when performed, runs the original action multiple times, collecting the results into a list:

    replicateM 10 $ randomRIO (0, 10)
    

    You can also obtain similar results manually using do-notation.

    For more information, you should read up on monads, for example in Learn You a Haskell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I'm currently designing an application where I will need to use
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have a simple Login servlet that checks the passed name and
Let's say I need a 3-digit number, so it would be something like: >>>
Let's say that I have an arbitrary string like `A man + a plan
Let's say I have two files.. I want to compare them side-by-side and see
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say i have an android device that has some extra buttons on it,

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.