0

I am using the following Haskell code to write a file of one million pseudorandom bits:

import System.Random
rbits= do
  g <- getStdGen
  writeFile "haskellbits.txt" (take 1000000 (randomRs ('0', '1') g))

However, I am also interested in writing the seed used to get the sequence. How can I output it?

  • 1
    Well, `StdGen` has a `Show` instance. You can simply use `print g`. (Please indent your code correctly and make sure you've copied everything) – Zeta Jan 20 '15 at 00:18
  • Also, consider using a different generator. Haskell no longer specifies an official one and `StdGen` has many deficiencies. – Thomas M. DuBuisson Jan 20 '15 at 01:56
  • I'm trying to compare different generators to see which is best in which scenarios. @Thomas what sort of deficiencies does `stdGen` have? – user2962685 Jan 20 '15 at 21:34
  • @user2962685 `StdGen` is not cryptographically secure, `StdGen` [isn't even statistically good random](https://github.com/GaloisInc/cryptol/issues/86), it is really slow compared to some generators, by default it seeds from just the time, you can't reseed the generator, the available abstraction (`RandomGen`) does not allow for failure, `RandomGen` doesn't even allow for creation of a new geneator, and there is no tracking of the period. – Thomas M. DuBuisson Jan 20 '15 at 22:29

1 Answers1

1
import System.Random
outputSeed = do
  g <- getStdGen
  print g

This will print out the seed as two numbers (I just tried it and it output 1010512508 1)

You can also use read to turn the string back into a random seed. If the original string is in that format, it will return the same seed. But if you use some other string it will do something else to generate the seed.

Prelude System.Random> read "111 112" :: StdGen
111 112
Prelude System.Random> read "blabla" :: StdGen
37214 1
Jeremy List
  • 1,756
  • 9
  • 16