Created
November 6, 2016 00:40
-
-
Save tugberkugurlu/1b227e0500f352ac2183256f489c485e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let randomString len = | |
let chars = "ABCDEFGHIJKLMNOPQRSTUVWUXYZ0123456789" | |
let charsLength = chars.Length | |
let random = System.Random() | |
let randomChars = [|for i in 0..len -> chars.[random.Next(charsLength)]|] | |
new System.String(randomChars) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let randomString = | |
let chars = "ABCDEFGHIJKLMNOPQRSTUVWUXYZ0123456789" | |
let charsLength = chars.Length | |
let random = System.Random() | |
fun len -> | |
let randomChars = [|for i in 0..len -> chars.[random.Next(charsLength)]|] | |
new System.String(randomChars) |
Perhaps this clarifies - this is equivalent to utils2:
let randomString =
let chars = "ABCDEFGHIJKLMNOPQRSTUVWUXYZ0123456789"
let charsLength = chars.Length
let random = System.Random()
// create the function
let generateString len =
let randomChars = [|for i in 0..len -> chars.[random.Next(charsLength)]|]
new System.String(randomChars)
// now return the function
generateString
I didn't know String
could take an array of char
in the constructor, that's a convenient trick :)
Instead of
// omitted
let randomChars = [| for i in 0..len -> chars.[random.Next(charsLength)] |]
new System.String(randomChars)
... you could then go all out and do:
[| for i in 0..len -> chars.[random.Next(charsLength)] |]
|> System.String
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see https://twitter.com/neoeinstein/status/795068069219631105