How can I defined the width of a random number?
let randomNumber = System.Random()
let randomBarcodePart2 = randomNumber.Next(**000000, 999999**)
I have this now, but is it possible to generate a random number, where we only defined the width.
Like this:
let randomBarcodePart2 = randomNumber.Next(6)
simple example with printf:
let t = randomNumber.Next(0,999999)
printfn "%07i" t
You can do this with a type extension:
type System.Random with
member x.Width w =
x.Next(0, pown 10 w - 1)
|> sprintf "%0*d" w
Here's a few outputs
Random().Width 6;;
val it : string = "985326"
Random().Width 9;;
val it : string = "803426947"
Random().Width 1;;
val it : string = "6"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.