Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

เชี่ยวชาญ Random Numbers และ Strings ใน Ruby: คู่มือปฏิบัติจริง

อยากเรียนรู้การสร้าง random numbers และ strings ใน Ruby? คุณมาถูกที่แล้ว!

เพราะบทความนี้พูดถึงเรื่องนี้โดยตรง

ด้วย random number คุณสามารถ สุมสมาชิกจาก array, เลือกผู้ชนะจากรายการ, สุ่มทอยลูกเต๋า ฯลฯ

ใน Ruby มีหลายวิธีสร้าง random numbers ด้วยคุณสมบัติต่างๆ

ตัวอย่าง...

เมธอด rand ใช้ได้ 3 แบบ:

  • ไม่มี argument: rand คืน float 0-1 (เช่น 0.4836732493)
  • รับ integer argument: rand(10) คืน integer 0 ถึงตัวเลขนั้น
  • รับ range argument: rand(1..20) คืน integer ในช่วง range

วิธีอื่นๆ สร้าง randomness:

  • เมธอด Array#shuffle
  • เมธอด Array#sample
  • Class SecureRandom

มาดูตัวอย่างกัน!

การสร้าง Random Numbers

สร้าง random numbers ด้วย rand:

เชี่ยวชาญ Random Numbers และ Strings ใน Ruby: คู่มือปฏิบัติจริง

rand ไม่มี argument คืน float (0.4836732493)

ส่ง argument เข้า rand ได้เลข 0 ถึง (แต่ ไม่รวม) ตัวเลขนั้น

 rand 100 > 41 

การสุ่มเลขใน Ruby ง่ายมาก แต่ถ้าอยากได้เลขใน range เฉพาะ ไม่ได้เริ่มจาก 0?

ไม่เป็นไร! ใช้ range ได้เลย

ตัวอย่าง:

 rand 200..500 > 352 

Secure Ruby Random Numbers

เลขจาก rand พอสำหรับแอปง่ายๆ...

...แต่ถ้าต้องการเพื่อความปลอดภัย —เช่นสร้าง password reset token— ใช้ SecureRandom (standard library)

SecureRandom ใช้ seed จาก /dev/urandom (Unix) หรือ CryptAcquireContext/CryptGenRandom (Windows)

ตัวอย่าง:

 require 'securerandom' SecureRandom.random_number > 0.232 

ทำงานคล้าย rand ส่ง max number ได้ด้วย

ตัวอย่าง:

 SecureRandom.random_number(100) > 72 

SecureRandom มี output formats อื่นๆ

ใช้ hex สร้าง fixed-width hex string:

 SecureRandom.hex > "87694e9e5231abca6de39c58cdfbe307" 

Ruby 2.5 เพิ่มเมธอดใหม่ สร้าง alphanumeric strings แบบสุ่ม:

 SecureRandom.alphanumeric > "PSNVXOeDpnFikJPC" 

การสุ่มสมาชิกจาก Arrays

อยาก สุมสมาชิกจาก list?

ลอง这样:

 [5, 15, 30, 60].shuffle.first > 30 

แต่ Ruby มี sample เหมาะกว่า (และเร็วกว่า):

 [5, 15, 30, 60].sample > 5 

ใช้ sample กับ ranges ได้ สร้างตัวอักษรสุ่ม:

 ('a'..'z').to_a.sample > b 

ส่ง integer argument ให้ sample เพื่อได้ N unique elements:

 [1,2,3].sample(2) > [1, 2] 

ส่ง custom random generator เป็น argument ได้:

 [1,2,3].sample(random: SecureRandom) 

Ruby Random Strings

การประยุกต์สุ่มสูงสุด: สร้าง random string ด้วย custom character set โค้ด:

def generate_code(number) charset = Array('A'..'Z') + Array('a'..'z') Array.new(number) { charset.sample }.join end puts generate_code(20)

มีรายละเอียดหลายอย่าง:

เตรียม charset ด้วย ranges แปลงเป็น arrays ใช้ Array.new กับ block สร้าง array ขนาด n ด้วยค่าจาก block

ผลลัพธ์แบบ: TufwGfXZskHlPcYrLNKg

ปรับ character set ได้ตามต้องการ

Seeding Random Number Generator

อยากควบคุมเลขที่สุ่มได้? ตั้ง seed ผ่าน srand

Seed คืออะไร?

Seed คือตัวเลขเริ่มต้นลำดับ random numbers ทุกเลข มาจาก seed นี้

คุณภาพ seed สำคัญต่อลำดับที่ดี Ruby จัดการให้แล้ว (ใช้ SecureRandom ถ้าต้องการความปลอดภัย) แต่บางกรณี (testing/debugging) อยากตั้ง seed เอง

ทำได้ด้วย:

 Kernel.srand(1) 

Seed 1 จะได้ลำดับแน่นอน:

 Array.new(5) { rand(1..10) } # [6, 9, 10, 6, 1] 

ตั้ง seed 1 อีกครั้ง ลำดับจะเริ่ม 6, 9, 10... ซ้ำเดิม

สรุป

เสร็จแล้ว! คุณพร้อมใช้ randomness ในโปรแกรม Ruby 🙂

บทความมีประโยชน์?

แชร์ให้เพื่อนๆ และ สมัครรับ newsletter เพื่อไม่พลาดเนื้อหาใหม่!