如何在Elm中随机生成一对花车?

时间:2016-05-20 11:04:56

标签: random elm

我正在尝试创建一个-- For HOME SELECT p.PersonKey, pn.Phone_Number, pn.Tel_NumberKey FROM Persons AS p INNER JOIN xref_Person+Telephone AS x ON p.PersonKey = x.PersonKey INNER JOIN Telephone AS pn ON x.Tel_NumberKey = pn.Tel_NumberKey WHERE pn.Tel_NumberType = 1 -- e.g. Home phone number AND pn.Tel_NumberKey = (SELECT MAX(pn1.Tel_NumberKey) AS Tel_NumberKey FROM Person AS p1 INNER JOIN xref_Person+Telephone AS x1 ON p1.PersonKey = x1.PersonKey INNER JOIN Telephone AS pn1 ON x1.Tel_NumberKey = pn1.Tel_NumberKey WHERE pn1.Tel_NumberType = 1 AND p1.PersonKey = p.PersonKey AND pn1.Tel_Number = pn.Tel_Number) ORDER BY p.PersonKey ,其中第一个Random.Generator (Float, Float)在某个范围内说Float 0.0,第二个在1000.0和第一个值之间

有效值示例:0.0 无效:(12.0, 5.0)

我查看了Random.Extra模块中的代码,但我找不到自己的方式。

示例下面甚至没有编译。 :)

(303.0, 800.0)

2 个答案:

答案 0 :(得分:4)

这是生成器代码:

floatTuple : Random.Generator (Float, Float)
floatTuple =
    Random.float 0.0 1000.0 
    `Random.andThen`
    (\val1 -> Random.map ((,) val1) (Random.float 0 val1))

以下是你如何使用它(在0.17中):

(tuple, nextSeed) = Random.step floatTuple initialSeed

答案 1 :(得分:4)

您需要使用Random.andThen

以下是一个完整的工作示例(您可以将其复制并粘贴到http://elm-lang.org/try

import Html exposing (..)
import Html.App as App

import Random exposing (Generator, generate, float, map, andThen) 
import Time exposing (every, second) 

myPair : Float -> Float -> Generator (Float, Float) 
myPair min max =
  let 
    range = float min max 
  in 
    range 
    `andThen` (\f -> map ((,) f) (float min f)) 


type Msg = Request | Update (Float, Float) 

update msg model = 
  case msg of 
    Request -> model ! [generate Update (myPair 0 1000)]
    Update pair -> pair ! []


view model = text (toString model) 


main =
  App.program 
    { init = (0,0) ! []
    , update = update 
    , view = view 
    , subscriptions = (\_ -> every (2*second) (always Request))
    }