2017-10-07 82 views
0

我正在玩弄榆樹並創建一個密碼生成器作爲玩具練習。在可打印的ascii'set'中生成字符。目前我正在使用以下命令:如何在榆樹中生成可表示的ascii字符?

Random.generate NewRandomChar (Random.Char.ascii)) 

但是這會產生大量不可打印的字符(例如NUL)。如果相反,我使用Random.Char.latin,我不會得到數字或符號像感嘆號,因此不適合密碼生成器。 有沒有一種簡單的方法在ascii範圍dec33到dec126中獲得一個字符?

謝謝!

的完整代碼:https://github.com/jumpifzero/snippets/blob/master/experiments/elm/pwd-generator.elm

編輯: 只是爲了完整性我添加了一個簡單的謂詞來檢查字符是在需要的範圍內,但不愛了解決方案:/任何幫助清潔此感謝! https://github.com/jumpifzero/snippets/blob/master/experiments/elm/pwd-generator2.elm

回答

2

您正在使用elm-community/random-extra包,其中包括使用char給定的範圍之間的隨機字符發生器:

Random.generate NewRandomChar (Random.Char.char 33 126)) 

你可以很容易地推出自己的功能,而不需要額外的軟件包:

import Char exposing (fromCode) 
import Random exposing (Generator, map, int) 

passwordCharGenerator : Generator Char 
passwordCharGenerator = 
    map fromCode (int 33 126)