Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm - producing a list of random number changing with time

I am trying to make a column of random numbers changing every second, but I get different error messages:

import Random

main = flow down 
[ asText (Random.range 0 100 (every second))  
, asText (Random.range 0 100 (every second))
]

gives a parse error. What is wrong with my square bracket [?

Parse error at (line 5, column 1):
unexpected '['
expecting newline, spaces or end of input

Indent Maybe?

Once I indent, the example does compile but I just get <signal> instead of the actual number

main = flow down 
  [ asText (Random.range 0 100 (every second))  
  , asText (Random.range 0 100 (every second))
  ]

lift for signals?

Finally when I tried to use lift it gives me other confusion

main = flow down 
  [ lift asText (Random.range 0 100 (every second))  
  , lift asText (Random.range 0 100 (every second))
  ]

The error message is that I have the wrong type for lift.

Type error on line 5, column 5 to 9:
       lift

  Expected Type: Signal Element
    Actual Type: Element

No flow down just a list

If I forget flow down it still doesn't cooperate:

main = lift asText
  [  (Random.range 0 100 (every second))  
  ,  (Random.range 0 100 (every second))
  ]

I get an error message that _List was expected:

Type error between lines 5 and 7:
       [Random.range 0 100 (every second),
        Random.range 0 100 (every second)]

  Expected Type: _List
    Actual Type: Signal

?

Am I using Random.range correctly? I have not changed it from the original example:

  • http://elm-lang.org/edit/examples/Reactive/Randomize.elm

How do I get it to cooprate with lift and flow down ?

like image 764
john mangual Avatar asked Jul 25 '14 16:07

john mangual


People also ask

How are random numbers generated from a seed?

A random seed specifies the start point when a computer generates a random number sequence. This can be any number, but it usually comes from seconds on a computer system's clock (Henkemans & Lee, 2001). A computer counts seconds from January 1, 1970 — a system called Unix time.

What is randomness code?

Updated: 11/30/2020 by Computer Hope. The term random refers to any collection of data or information with no determined order, or is chosen in a way that is unknown beforehand. For example, 5, 8, 2, 9, and 0 are single-digit numbers listed in random order.


1 Answers

Here's an answer that works with 0.15 [EDIT: and 0.16], currently the latest version of Elm. Since Joe's answer was written, the Random library has been overhauled completely to use a pure random number generator. The pseudorandom numbers are deterministic: every run is always the same, unless you change the initial seed.

We start with imports: boring but necessary, and then define some constants using the Random library.

import Graphics.Element exposing (flow, down, show, Element)
import Time exposing (fps)
import Random

gen = Random.int 0 100
gen2 = Random.pair gen gen
seed0 = Random.initialSeed 42

Next we define a state type, containing the random seed and the numbers to display. I assumed we want two; for a list of constant length, use Random.list n gen. We also define an initial state using the record constructor syntax (and two "random" numbers).

type alias State = {seed : Random.Seed, first : Int, second : Int}
state0 = State seed0 36 89

Now we define a step function to be run once a second. Here we peel off two random numbers and store them, along with the new seed. Notice that we use a new seed each time, chained one to the next.

step : a -> State -> State
step _ state =
  let
    ((first, second), seed') = Random.generate gen2 state.seed
  in
    State seed' first second

Now we use foldp to introduce state, to actually run that step function.

state : Signal State
state = Signal.foldp step state0 (fps 1)

We define a pure render function. No signals here.

render : State -> Element
render state =
    flow down [show state.first, show state.second]

And finally we map (formerly lift) the render function on to the state.

main = Signal.map render state

If you concatenate the gray boxes and remove the interstitial comments, you will get a working Elm 0.15 program. But be advised that it appears to be CPU-intensive.

like image 58
mgold Avatar answered Sep 20 '22 14:09

mgold