Skip to content

Instantly share code, notes, and snippets.

@leikind
Created December 15, 2015 20:19
Show Gist options
  • Select an option

  • Save leikind/7344504ec639fc96e83b to your computer and use it in GitHub Desktop.

Select an option

Save leikind/7344504ec639fc96e83b to your computer and use it in GitHub Desktop.
A signal of random numbers every second where the first seed is a timestamp
module SignalOfRandomNum where
import Graphics.Element as GE
import Time
import Random
import Maybe exposing (..)
randomIntSignal : Int -> Int -> Signal Time.Time -> Signal Int
randomIntSignal lo hi inputSignal =
let
intGen = Random.int lo hi
nextRandom seed =
seed
|> Random.generate intGen
|> Just
update time accu =
case accu of
Just (_, seed) ->
seed
|> nextRandom
Nothing ->
round time
|> Random.initialSeed
|> nextRandom
in
inputSignal
|> Signal.foldp update Nothing
|> Signal.map (\maybe->
case maybe of
Just (n,_) -> n
Nothing -> -1 )
signalOfRandomIntEverySecond : Signal Int
signalOfRandomIntEverySecond =
Time.every Time.second |> randomIntSignal 1 100
main : Signal GE.Element
main =
Signal.map GE.show signalOfRandomIntEverySecond
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment