产生唯一的随机数 [英] Generating unique random number

查看:100
本文介绍了产生唯一的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想在读取json后三遍在init上生成5个唯一数字的列表。所以基本上我想得到类似[31,59,62,72,2,16,2,38,94,15,55,46,83,2,10]的内容。我面临的挑战是我对函数式编程和elm还是陌生的,我有点迷茫。所以我知道Random.generate需要一个味精和一个生成器并返回一个cmd消息,它主要用于更新功能,但这不是我需要的,因为它是一个辅助功能,不需要与客户端进行通信。我认为它可以在init中使用,但我不知道如何使用。再次,我的函数是递归的,我不知道如何使用Random。递归地生成此逻辑。

I have an application that I want to generate a list of 5 unique numbers on init three times after reading a json. So basically I want to get something like [31, 59, 62, 72, 2, 16, 2, 38, 94, 15, 55, 46, 83, 2, 10]. My challenge is that I am new to functional programming and elm and I am a bit lost. So I understand that Random.generate takes a msg and a generator and returns a cmd message and it is mostly used in the update function but this is not where I need as it is a helper function and doesn't need to communicate with the client. I think it can be used in init but I don't know how. Again my function is recursive and I don't know how to apply this logic with Random.generate recursively.

我知道我的代码无法正常工作,我已经尝试过了,因为Random.int不会生成随机数,而是生成的一种类型,但是我仍然不知道如何应用它来获取我想要的东西。

I understand my code will not work and I have tried it because Random.int does not generate a random number but a type of generate but still I don't know how to apply this to get what I want.

recursion : Int -> List a -> List number
recursion a b =
  if List.length b > 5
      then b
  else 
      let 
          rand = Random.int 0 a
      in
          if(List.member rand b)
              then recursion a b
          else
              recursion a (rand :: b)

调用:

recursion 50 []

我想生成5次唯一随机3次的列表/数组。

I want to generate a list/array of 5 unique random 3 times.

推荐答案

好问题。这里有两个部分:
-生成随机数,和
-将所有内容连接起来

Great question. There are two parts here: - generating random numbers, and - wiring this all up

对于前者,您将需要Random库,稍加检查便会导致您进入

You will need the Random library for the former, and a little inspection will lead you to something like

get15Randoms = Random.generate OnRandomNumbers <| Random.list 5 (int 0 100)

此类型为 Cmd Msg -这是一个异步操作,将在消息上返回。

This has type Cmd Msg - it's an async operation that will return on a Msg.

将其连接起来将经历一系列阶段。您指的是在 init中执行此操作,但这并不是Elm在这里为您工作的方式。在init函数中,您可以启动json请求。然后您将需要类似

Wiring it up will be a series of stages. You refer to doing it in 'init' but that's not how Elm is going to work for you here. In the init function you can start off your json request. Then you'll need something like

init = ( initModel
       , Http.get {url =..., expect = Http.expectJson OnJson yourDecoder} 
       )

update msg model = 
    case msg of 
        OnJson (Ok data) ->
            -- attach json
            ( {model | data = data }, get15Randoms )
        OnRandomNumbers ints ->
            ( { model | ints = ints }, Cmd.none )

json回来后,您可以附加它,并使用该更新的迭代启动您的随机数请求,然后在后续迭代中捕获结果

In other words, once the json comes back you can attach that, and use that iteration of the update to launch your random number request, and then catch the result in a subsequent iteration

这篇关于产生唯一的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆