本文簡要介紹ruby語言中 Random::Formatter模塊
的用法。
Random
數字格式化程序。
格式以多種方式生成隨機數。
例子
生成隨機十六進製字符串:
require 'random/formatter'
prng.hex(10) #=> "52750b30ffbc7de3b362"
prng.hex(10) #=> "92b15d6c8dc4beb5f559"
prng.hex(13) #=> "39b290146bea6ce975c37cfc23"
生成隨機 base64 字符串:
prng.base64(10) #=> "EcmTPZwWRAozdA=="
prng.base64(10) #=> "KO1nIU+p9DKxGg=="
prng.base64(12) #=> "7kJSM/MzBJI+75j8"
生成隨機二進製字符串:
prng.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
prng.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
生成字母數字字符串:
prng.alphanumeric(10) #=> "S8baxMJnPl"
prng.alphanumeric(10) #=> "aOxAg8BAJe"
生成 UUID:
prng.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
prng.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
像 Random
那樣在給定範圍內生成一個隨機數
prng.random_number #=> 0.5816771641321361
prng.random_number(1000) #=> 485
prng.random_number(1..6) #=> 3
prng.rand #=> 0.5816771641321361
prng.rand(1000) #=> 485
prng.rand(1..6) #=> 3
相關用法
- Ruby Formatter.random_bytes用法及代碼示例
- Ruby Formatter.urlsafe_base64用法及代碼示例
- Ruby Formatter.uuid用法及代碼示例
- Ruby Formatter.base64用法及代碼示例
- Ruby Formatter.hex用法及代碼示例
- Ruby Formatter.alphanumeric用法及代碼示例
- Ruby Formatter.choose用法及代碼示例
- Ruby Float arg()用法及代碼示例
- Ruby File.identical?用法及代碼示例
- Ruby Fiber.schedule用法及代碼示例
- Ruby Float fdiv()用法及代碼示例
- Ruby Float quo()用法及代碼示例
- Ruby Float angle()用法及代碼示例
- Ruby FileUtils.mkdir用法及代碼示例
- Ruby Float divmod()用法及代碼示例
- Ruby Float.self - other用法及代碼示例
- Ruby Float.rationalize用法及代碼示例
- Ruby Float.truncate用法及代碼示例
- Ruby FileUtils.compare_file用法及代碼示例
- Ruby FileUtils.options_of用法及代碼示例
- Ruby File.dirname用法及代碼示例
- Ruby Float prev_float()用法及代碼示例
- Ruby FileUtils.ln_s用法及代碼示例
- Ruby Float round()用法及代碼示例
- Ruby Float denominator()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Formatter模塊。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。