當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Random random_number()用法及代碼示例


Random#random_number():random_number()是一種隨機類方法,該方法檢查從原始隨機字節中返回一個隨機數

用法:Random.random_number()

參數:隨機值


返回:來自原始隨機字節的隨機數

示例1:

# Ruby code for Random.random_number() method 
  
# loading library 
require 'securerandom'
  
# declaring Random value 
date_a = SecureRandom.random_number 
  
# declaring Random value 
date_b = SecureRandom.random_number 
  
# random_number value 
puts "Random random_number form : #{date_a}\n\n"
  
puts "Random random_number form : #{date_b}\n\n"

輸出:

Random random_number form : 0.06094009368827358

Random random_number form : 0.23398922898567265

示例2:

# Ruby code for Random.random_number() method 
  
# loading library 
require 'securerandom'
  
# declaring Random value 
date_a = SecureRandom.random_number(10) 
  
# declaring Random value 
date_b = SecureRandom.random_number(200) 
  
# random_number value 
puts "Random random_number form : #{date_a}\n\n"
  
puts "Random random_number form : #{date_b}\n\n"

輸出:

Random random_number form : 6

Random random_number form : 82



相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Random random_number() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。