<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 28 Jan 2009, at 04:33, Erick González wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div bgcolor="#ffffff"><div><font face="Arial" size="2">Hi:</font></div><div><font face="Arial" size="2">Haskell' s way of random number generation is strange to me, but I know how to do that. I' d like to know how can I call random numbers generated on the screen&nbsp;inside a classic function. I mean, I need to know the way of calling random numbers (one different every time that I run) inside a function. Please, Could anybody help me?</font></div></div></span></blockquote><br></div><div>There's a problem with doing this – Haskell guarentees that no matter what (well, okay, not quite, but unsafePerformIO doesn't count), if you give the same arguments to a function, you'll get the same results. &nbsp;This is known as referential transparency. &nbsp;This is what gets us some of the biggest gains from the language (e.g. without it, we wouldn't be allowed to use our choice of non-strict evaluation order, we wouldn't be allowed to automatically parallelise, etc).</div><div><br></div><div>So, how is this problem solved? &nbsp;In a couple of interesting ways. &nbsp;First is the way you've already seen, we can lock random number generation in a monad (usually the IO monad). &nbsp;What this does, is it guarentees that the same IO action will always be returned by the function, but when that IO action gets shoved into the runtime, it can do any non-referentially transparent thing it likes, but that's "okay", because we're out of haskell land.</div><div><br></div><div>Understandably, this isn't the nicest way to do things, because it locks us in the IO monad, and in the evaluation of the action we lose all the advantages we had. &nbsp;Not only that, but the programming style is fairly imperative. &nbsp;So, we can think of a couple of ways round the restruction – in order to give different outputs, we must have different inputs, so lets think about a couple of ways of doing that:</div><div><br></div><div>Firstly, we can pass a 'seed' into our pure function with which to generate further random numbers. &nbsp;Secondly, we can use Haskell's ability to deal with infinitely large data structures, and pass in an infinitely long list of 'random' numbers. &nbsp;Here's the second one:</div><div><br></div><div>main = do</div><div>&nbsp;&nbsp;seed &lt;- do something to generate a seed</div><div>&nbsp;&nbsp;interact $ pureMain . randoms $ seed</div><div><br></div><div>pureMain :: [Int] -&gt; String -&gt; String</div><div>pureMain rs "jam" = show (take 20 rs)</div><div><br></div><div>Hope that helps</div><div><br></div><div>Bob</div></body></html>