About Random Integer without IO
Hi, you need to reallise that Haskell is a pure language. That means a function has the same result if you call it with the same arguments (deterministic). A function that produces random numbers are not of such kind in the first place. However, what you need to do is to pass the random number generator though the function: See e.g. System.Random.random [1] which takes the generator and gives you a random value and the new generator back. Georg [1] http://www.haskell.org/ghc/docs/latest/html/libraries/base/System.Random.htm... PS: this question is more appropriate for haskell-cafe. On Thu, 11 Nov 2004 14:40:35 +0000, Jose manuel Hernando Cobeña <elreyjoseman@hotmail.com> wrote:
I need generate random numbers by create polygons with "wxHaskell", I am searching in the web, but all I only find IO functions like
test :: Integer -> IO Integer test n = do x <- randomRIO (1, n) return x I need this but with types :: Integer -> Integer
Thanks
Escapadas, fines de semana, vacaciones, reservas. Organiza y contrata tus viajes aquí.
_______________________________________________
Haskell mailing list
Haskell@haskell.org
-- ---- Georg Martius, Tel: (+49 34297) 89434 ---- ------- http://www.flexman.homeip.net ---------
Georg Martius answers the request of: Jose Manuel Hernando Cobeña
I need generate random numbers by create polygons with "wxHaskell", I am searching in the web, but all I only find IO functions like
test :: Integer -> IO Integer
...
I need this but with types :: Integer -> Integer
you need to reallise that Haskell is a pure language. That means a function has the same result if you call it with the same arguments (deterministic). A function that produces random numbers are not of such kind in the first place.
All depends on how you organize your computations. 1. You may in a purely functional, non-monadic way produce an infinite list, a stream of numbers, and use them as you wish, incrementally. gener seed =(aaa*seed + ccc) `mod` mmm -- or something alike... strm = st seed0 where st x = x : st (gener x) Eventually, you can directly pass the updated seed from one function to another in your program. In two words, write your programs in CPS, or a little 'monadized' style. Of course, you can transform the result from an integer between 0 and mmm-1 to other thing. 2. Despite what G. Martius says, you CAN USE a pure, deterministic function to produce something which looks randomly, it suffices to change the argument. Concretely, Ward in Graphics Gems, and other people in other places propose 'chaotic', *ergodic* functions, which vary wildly from arg to arg, even neighbouring. This is a "kind of hashing function". An example can be found in this tutorial http://freespace.virgin.net/hugo.elias/models/m_perlin.htm You take an integer, and mix its bits by taking xors, modulo, polynomials, etc. Then, such a function sampled for j=0,1,2,3, ... N will look like a genuinely random signal, without any need to 'propagate' anything. Jerzy Karczmarczuk PS. Georg Martius concludes:
PS: this question is more appropriate for haskell-cafe.
Well, why not haskell-bratwurst-mit-pils? The protocol of making/using random stuff within Haskell is a specific problem which is not very well developed nor taught, but it is not anecdotical, it may be important.
This method unfortunately depends on having a seed first though. One must use a different value every time the program is started, commonly time or the first few bytes from /dev/random. Any one of these is going to require a monadic function to generate (i.e. it must come from the environment in some way - it must change every time you run the program) So while this eliminates the IO monad from most of the program, it must still be initiated using it. Thanks Tom Davie -- Computer Science is as much about computers as astronomy is about telescopes -- Edsgar Dijkstra On 11 Nov 2004, at 20:58, karczma wrote:
Georg Martius answers the request of: Jose Manuel Hernando Cobeña
I need generate random numbers by create polygons with "wxHaskell", I am searching in the web, but all I only find IO functions like
test :: Integer -> IO Integer ... I need this but with types :: Integer -> Integer
you need to reallise that Haskell is a pure language. That means a function has the same result if you call it with the same arguments (deterministic). A function that produces random numbers are not of such kind in the first place.
All depends on how you organize your computations. 1. You may in a purely functional, non-monadic way produce an infinite list, a stream of numbers, and use them as you wish, incrementally. gener seed =(aaa*seed + ccc) `mod` mmm -- or something alike... strm = st seed0 where st x = x : st (gener x) Eventually, you can directly pass the updated seed from one function to another in your program. In two words, write your programs in CPS, or a little 'monadized' style. Of course, you can transform the result from an integer between 0 and mmm-1 to other thing. 2. Despite what G. Martius says, you CAN USE a pure, deterministic function to produce something which looks randomly, it suffices to change the argument. Concretely, Ward in Graphics Gems, and other people in other places propose 'chaotic', *ergodic* functions, which vary wildly from arg to arg, even neighbouring. This is a "kind of hashing function". An example can be found in this tutorial http://freespace.virgin.net/hugo.elias/models/m_perlin.htm You take an integer, and mix its bits by taking xors, modulo, polynomials, etc. Then, such a function sampled for j=0,1,2,3, ... N will look like a genuinely random signal, without any need to 'propagate' anything.
Jerzy Karczmarczuk PS. Georg Martius concludes:
PS: this question is more appropriate for haskell-cafe.
Well, why not haskell-bratwurst-mit-pils? The protocol of making/using random stuff within Haskell is a specific problem which is not very well developed nor taught, but it is not anecdotical, it may be important. _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Thomas Davie writes:
This method unfortunately depends on having a seed first though.
Which "this method"? Please, quote the text you are referring to *before* your answer.
One must use a different value every time the program is started, commonly time or the first few bytes from /dev/random. Any one of these is going to require a monadic function to generate (i.e. it must come from the environment in some way - it must change every time you run the program)
So while this eliminates the IO monad from most of the program, it must still be initiated using it.
I understand that you mean the seed-propagation method. You are *partly* right, that the same program executed several times will generate the same sequence, if initialized identically. But there is no need for monads, it suffices to pass a parameter, or to read a different initialization file each time. Of course, anybody, especially somebody contributing to this list has the right to be a nit-picker. But, if one is a *practical* user of random stuff, then he/she usually knows such details. Moreover, it is sometimes very useful to control absolutely the initialization, to be able to repeat the experiment, so, professionals in Monte Carlo, etc. rarely if ever, use time, or system-dependent (and hidden) random values. For me the initialization is a *context* problem, which I separate from the properties of the random generator. It is its quality, lack of correlations, etc. which is important. Do you know of many problems requiring an automated, truly random initialization? In this case one may well delegate this issue to the *launching* of the functional program, instead of struggling with it from within. This is my personal philosophy, everybody is entitled to his/hers. Jerzy Karczmarczuk
participants (4)
-
Georg Martius -
Jose manuel Hernando Cobeña -
karczma -
Thomas Davie