Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rando
Search
Tim Uruski
May 14, 2014
Programming
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rando
A lightning talk in which we learn a bit about Ruby by working with random values.
Tim Uruski
May 14, 2014
More Decks by Tim Uruski
See All by Tim Uruski
Exceptional Ruby
timuruski
0
110
Structure and Interpretation of Ruby Programs
timuruski
1
260
Rack and Middleware
timuruski
0
210
From Whence Rubygems - Mark 2
timuruski
0
170
The Waiting Game
timuruski
0
120
From Whence Rubygems
timuruski
1
100
Other Decks in Programming
See All in Programming
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
220
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
680
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
490
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
130
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
560
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
250
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.6k
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
400
React本体のコードリーディング
high_g_engineer
1
140
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
230
数百円から始めるRuby電子工作
tarosay
0
140
Built Our Own Background Agent at LayerX
layerx
PRO
10
5.3k
Featured
See All Featured
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
230
VelocityConf: Rendering Performance Case Studies
addyosmani
332
25k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
270
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
990
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.3k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
430
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
790
Building AI with AI
inesmontani
PRO
1
1.1k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.5k
Transcript
Rando # Ruby v2.1.1 Random.srand 123 1
four random things 2
pick a random number between 100 and 200 3
(rand * 100 + 100).to_i => 169 4
(rand * 100 + 100).to_i => 169 NO! 5
rand(100..200) => 166 6
rand(*args) 7
how many kinds of random does Ruby have? 8
ObjectSpace .each_object .select { |obj| obj.respond_to?(:rand) } 9
Kernel Random #<Random:0x007fed39068820> 10
Kernel Random #<Random:0x007fed39068820> Random::DEFAULT => #<Random:0x007fed39068820> 11
Kernel Random #<Random:0x007fed39068820> 12
rand == Kernel.rand Random.rand == Random::DEFAULT.rand Kernel.rand != Random.rand 13
Kernel.rand => 0.6964691855978616 Random.rand => 0.6964691855978616 14
Kernel.rand(100) => 66 Random.rand(100) => 66 15
Kernel.rand(0..100) => 66 Random.rand(0..100) => 66 16
Kernel.rand(100.0) => 66 Random.rand(100.0) => 69.64691855978616 17
Kernel.rand(-100.0) => 66 Random.rand(-100.0) => ArgumentError 18
know your rand 19
generate a list of random numbers 20
(0..20).map { rand } => [0.696469, 0.286139, ...] 21
(0..20).map { rand } => [0.696469, 0.286139, ...] NO! 22
Array.new(20) { rand } => [0.696469, 0.286139, ...] 23
Array.new(*args) 24
Array.new(size, obj, &generator) 25
Array.new(3) => [nil, nil, nil] 26
Array.new(3, 0) => [0, 0, 0] 27
Array.new(3) { |n| n } => [0, 1, 2] 28
know your primitives 29
pick a random subset of elements from an array 30
users = %w[alice bob carol] subset = Array.new(2) { users[rand
* users.length] } => ['bob', 'alice'] 31
users = %w[alice bob carol] subset = Array.new(2) { users[rand
* users.length] } => ['bob', 'alice'] NO! 32
users = %w[alice bob carol] users.sample(2) => ['bob', 'carol'] 33
Array#sample(*args) 34
Array#sample(size, random: rng) 35
a = [1,2,3,4,5] a.sample => 3 36
a = [1,2,3,4,5] a.sample(3) => [3, 1, 5] 37
a = [1,2,3,4,5] a.sample(1) => [3] 38
a = [1,2,3,4,5] rng = Random.new(234) a.sample(random: rng) => 1
39
a = [1,2,3,4,5] rng = Random.new(234) a.sample(3, random: rng) =>
[1, 5, 2] 40
a = [1,2,3,4,5] rng = Random.new(234) a.shuffle(random: rng) => [3,
2, 5, 4, 1] 41
know your primitives (seriously) 42
generate a secure token from random values 43
token = '' 40.times do token << rand(0..255).to_s(16) end =>
'fe6d7e42dc62e6115...' 44
token = '' 40.times do token << rand(0..255).to_s(16) end =>
'fe6d7e42dc62e6115...' NO! 45
require 'securerandom' token = SecureRandom.hex(40) => '6011e8861f024cb75...' 46
require 'securerandom' token = SecureRandom.hex(40) => '6011e8861f024cb75...' => '8f90ea2187b50cb52...' =>
'cad2eb20a39939601...' => '272a822296de5b249...' => 'ad65839e646c27252...' 47
know your security (seriously) 48
hopefully you didn't learn anything 49