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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:46:43+00:00 2026-05-18T10:46:43+00:00

So I’m writing a roguelike and I need to generate some dungeons randomly. I

  • 0

So I’m writing a roguelike and I need to generate some dungeons randomly. I could use system.random but I really want to make it pure. I want to use newPureMT to generate a mersenne twister random source, then pass it into some monad transformers like stateT and readerT and a few others. So that I would end up with something along the lines of:

genDungeon = do
  x <- getRandomNumbers
  genrooms x
  y <- getRandomNumbers
  gendoors x
  etc

I can’t figure out how to do this without staying in the IO monad. For example, it gives this example:

sample (uniform 1 100) :: State PureMT Int

Which means I should be able to:

blah = do
  x <- newPureMT
  runState genDungeon x

But even typing it into ghci yields the error

Overlapping instances for Data.Random.Lift.Lift
                            Data.Functor.Identity.Identity
                            (StateT PureMT Data.Functor.Identity.Identity)
  arising from a use of `sample' at <interactive>:1:0-28
Matching instances:
  instance [incoherent] (Monad m, MonadTrans t) =>
                        Data.Random.Lift.Lift m (t m)
    -- Defined in Data.Random.Lift
  instance [incoherent] (Monad m) =>
                        Data.Random.Lift.Lift Data.Functor.Identity.Identity m
    -- Defined in Data.Random.Lift

to which I have absolutely no idea what that means or how to fix it.

I’ve spent a few days trying to figure this out, and I’m just completly clueless. It is pretty obvious that there are some pretty heavy type signatures and lifting of some stuff that I have to apply to get stuff to work, but I cannot figure out what they are or how to do it.

Edit: Okay, so I was looking through the Mersenne Twister code and I saw that PureMT is an instance of randomgen which means that I can pass it into system.random and get pure code out of it, rendering random-fu unecessary. I still kind of wish I could figure out how to make this code work as random-fu gives you a ton of extra abilities like different random distributions, but his will do for my project.

Edit: Okay, so I ended up with this code:

rand :: (RandomGen g, MonadState g m) => Int -> Int -> m Int
rand lo hi = do
    r <- get
    let (val, r') = randomR (lo, hi) r
    put r'
    return val


gendungeons = replicateM 10 $ do
  x <- rand 0 24
  y <- rand 4 10
  z <- replicateM 10 $ rand 5 50
  let dungeon = makeadungeonpurelywiththeserandomvalues x y z
  return dungeon


test = do
  x <- newPureMT
  let dungeons = runState gendungeons x
  return dungeons

This pretty much allows me to do what I wanted syntactically, and it uses mersenne twister so it should be a heck of a lot faster. The only thing that bothers me is that on every single number generation, it will update the seed in the state monad and I have heard that it is really slow, although I don’t know why. But this should be enough for my purposes.

  • 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-18T10:46:43+00:00Added an answer on May 18, 2026 at 10:46 am

    I wont say this is a better answer than Paul Johnson but this is one way to do it if I understood what you wanted:

    import Control.Monad.State
    import System.Random
    
    -- utility function to hide plumbing of random generator types.
    rand :: (Random a, RandomGen g, MonadState g m) => a -> a -> m a
    rand lo hi = do
        r <- get
        let (val, r') = randomR (lo, hi) r
        put r'
        return val
    
    (flip runState) (mkStdGen seed) $ do
        x <- rand 0 24
        y <- rand 4 10
        z <- rand 5 50
        ...
        ...
    

    I’m using the standard random generator module since I’ve never used random-fu but the principle should be the same just make a rand action if it doesn’t exist in the library.

    I’m using type-classes to keep the code generic, you can use this with any monad (transformer) that is an instance of the MonadState type-class. You don’t have to use type-classes you can use your specific monad in the type of rand instead if you want.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
This could be a duplicate question, but I have no idea what search terms
I want to construct a data frame in an Rcpp function, but when I
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.