#apache #archlinux #asterisk #centos #debian #gentoo #haskell #kde #kubuntu #lisp #math #mysql #perl #python #ruby-lang #rubyonrails #suse #ubuntu #vim #wikipedia 0 1 2 3 4 5 6 7 8 9 10 11 12
Top Prev 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 Next
#haskell
<sendark> hmm
<nmessenger> iron32: you didn't pass in the same RNG did you?
<iron32> well can I pass a random number to RNG then ?
<nmessenger> The RNG is the thing you pull the random numbers out of. You call the various 'random*' functions to extract a value and get a new version of the RNG that will give you a different number.
<iron32> but to be honest conceptual I can't see creating a function that returns a random number without wrapping it in a Monad
<nmessenger> think of a StdGen as an infinite list of already-made numbers. You can construct one several ways, by using a specific seed value, for instance.
<nmessenger> > mkStdGen 8
<lambdabot> 9 1
<LoganCapaldo> > mkStdGen 42
<lambdabot> 43 1
<nmessenger> (mkStdGen someSeed) will always generate the same numbers given the same seed.
<LoganCapaldo> @src mkStdGen
<lambdabot> Source not found. I've seen penguins that can type better than that.
<bo> iron32: can I think it in this way? the pseudo rand gen has a long list of rand numbers stored internally and you pass in an index it returns the corresponding number back?
<LoganCapaldo> @src StdGen
<lambdabot> Source not found. I can't hear you -- I'm using the scrambler.
<nmessenger> you can use the newStdGen action to make a new generator in IO initialized using the clock or something suitable
<nmessenger> @type newStdGen
<lambdabot> IO StdGen
<LoganCapaldo> @src show :: StdGen -> String
<lambdabot> Source not found. My pet ferret can type better than you!
<LoganCapaldo> I'm having a lousy time of this :)
<nmessenger> LoganCapaldo: use @src Type method for class methods, though StdGen probably won't work.
<iron32> bo : Well I fail to see how that simulates a die roll when I have to give it a seed each time ?
<LoganCapaldo> @src StdGen show
<lambdabot> Source not found. Have you considered trying to match wits with a rutabaga?
<LoganCapaldo> 43 1 and 9 1 just seem like really odd impls of show :)
<bo> iron32: you r right.
<nmessenger> > do g1 <- newStdGen; let {(x, g2) = randomR (1,6) g1; (y, g3) = randomR (1,6) g2}; return (x,y,g3)
<lambdabot> add an instance declaration for (Typeable StdGen)
<allbery_b> normally you stuff the StdGen in a State (or StateT IO, etc.) monad, and pass it around thgat way
<nmessenger> ^^ something like that (modulo errors :)) is how you would use an IO action to generate two random numbers.
<allbery_b> the wiki has a MonadRandom that keeps the state for you, I think, as am alternative
<iron32> Which leaves me with the problem of how to add Int to an IO Int
<iron32> If I lift the Int to an IO Int can I add them ?
<LoganCapaldo> iron32, you got it backwards :)
<nmessenger> @type \i mi -> fmap (i+) mi
<lambdabot> forall a (f :: * -> *). (Num a, Functor f) => a -> f a -> f a
<allbery_b> do it inside IO. do n <- something_that_produces_IO_Int; return (n + 3)
<allbery_b> or liftM (+3) something_that_returns_IO_Int
<LoganCapaldo> you must temporarily pull the IO Int out add em and put the sum back in
<allbery_b> @wiki New_monads/MonadRandom
<lambdabot> http://www.haskell.org/haskellwiki/New_monads/MonadRandom
<nmessenger> iron32: you can either lift the addition into the IO Int, or use do syntax to pull the Int down and return the result back up.
<iron32> nmessenger : I though once you enter the Monad you can never get out ?
<nmessenger> iron32: right, that's why you need to return at the end of the do.
<LoganCapaldo> iron32, that actually depends on the monad
<allbery_b> you can temporarily escape. the usual way is to do something in IO then invoke pure code on it
<LoganCapaldo> but for IO yes, there is no escape :)
<allbery_b> that code will execute "within" the IO monad but you don't need to worry about it
Previous Page Next Page