I have previously created this function to generated integers random numbers in a range (m,n)
.
giveRand :: Random c => c -> c -> c
giveRand m n = unsafePerformIO . getStdRandom $ randomR (m,n)
From this situation I wanted to run it multiple times with the same parameters, so that it would return me a list of randomly generated values in the given range. I tried the replicate function, but it only did copy the result of giveRand. It did not create multiple copies of the function and reevaluate it.
From this problem I wondered if there is a function that allows me to run any function multiple times with the same parameters. I ask this for cases such as this one, that even with the same inputs of range, different values may arise.
So, is there any function in Haskell that enables me to run a function multiple times with the same parameters?
unsafePerformIO
are where you would find code that would allow you to turn "generate a random number" into "generate a list of random numbers".unsafePerformIO
is the source of your confusion. I very strongly recommend forgetting thatunsafePerformIO
exists. It's meant for expert users - the name is meant to scare you off! The situations where you need it are very rare indeed and this is not one of them. Expend your efforts on learning how to work with theIO
type instead. It's worth it!