Created
November 13, 2011 10:36
-
-
Save sreeix/1361967 to your computer and use it in GitHub Desktop.
Randomizing in Ruby with Redis/Cycle/Kernel.rand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'redis' | |
| @emails = ['foo@bar.com', 'bar@bar.com', 'baz@bar.com','car@bar.com'] | |
| @cycle_usage = {} | |
| @redis_usage = {} | |
| @random_usage = {} | |
| def reset | |
| @cycle_usage = {} | |
| @pos = 0 | |
| @redis_usage = {} | |
| @random_usage = {} | |
| end | |
| def cycle | |
| email = @emails[@pos % @emails.size] | |
| @pos+=1 | |
| @cycle_usage[email] = @cycle_usage[email] ? @cycle_usage[email] + 1 : 1 | |
| end | |
| def random | |
| email = @emails[Kernel.rand(@emails.size)] | |
| @random_usage[email] = @random_usage[email] ? @random_usage[email] + 1 : 1 | |
| end | |
| @redis = Redis.new | |
| @redis.del 'emails' | |
| @emails.each do |e| | |
| @redis.sadd 'emails', e | |
| end | |
| def redis_random | |
| item = @redis.srandmember 'emails' | |
| @redis_usage[item] = @redis_usage[item] ? @redis_usage[item]+1 : 1 | |
| end | |
| def percentages(hash, total) | |
| hash.collect do |k, v| | |
| [k, v.to_f/total.to_f] | |
| end | |
| end | |
| [10, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000].each do |i| | |
| puts '#################################################################' | |
| puts "At #{i} elements" | |
| puts "------------------Redis Random-----------------------" | |
| reset; i.times {redis_random} | |
| puts percentages( @redis_usage, i).inspect | |
| puts "------------------Cycle-----------------------" | |
| reset; i.times {cycle} | |
| puts percentages( @cycle_usage, i).inspect | |
| puts "------------------Randomm Ruby-----------------------" | |
| reset; i.times {random} | |
| puts percentages( @random_usage, i).inspect | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| althea/spaghetti/sf[cycling_email_addresses]% ruby script/random_test.rb | |
| ################################################################# | |
| At 10 elements | |
| ------------------Redis Random----------------------- | |
| [["foo@bar.com", 0.4], ["car@bar.com", 0.4], ["baz@bar.com", 0.1], ["bar@bar.com", 0.1]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.3], ["bar@bar.com", 0.3], ["baz@bar.com", 0.2], ["car@bar.com", 0.2]] | |
| ------------------Randomm Ruby----------------------- | |
| [["bar@bar.com", 0.2], ["car@bar.com", 0.4], ["baz@bar.com", 0.3], ["foo@bar.com", 0.1]] | |
| ################################################################# | |
| At 100 elements | |
| ------------------Redis Random----------------------- | |
| [["bar@bar.com", 0.14], ["car@bar.com", 0.36], ["foo@bar.com", 0.35], ["baz@bar.com", 0.15]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["bar@bar.com", 0.28], ["baz@bar.com", 0.26], ["car@bar.com", 0.21], ["foo@bar.com", 0.25]] | |
| ################################################################# | |
| At 200 elements | |
| ------------------Redis Random----------------------- | |
| [["foo@bar.com", 0.315], ["car@bar.com", 0.345], ["bar@bar.com", 0.225], ["baz@bar.com", 0.115]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["foo@bar.com", 0.295], ["baz@bar.com", 0.245], ["car@bar.com", 0.22], ["bar@bar.com", 0.24]] | |
| ################################################################# | |
| At 500 elements | |
| ------------------Redis Random----------------------- | |
| [["foo@bar.com", 0.378], ["baz@bar.com", 0.158], ["car@bar.com", 0.304], ["bar@bar.com", 0.16]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["bar@bar.com", 0.27], ["baz@bar.com", 0.21], ["foo@bar.com", 0.256], ["car@bar.com", 0.264]] | |
| ################################################################# | |
| At 1000 elements | |
| ------------------Redis Random----------------------- | |
| [["foo@bar.com", 0.317], ["car@bar.com", 0.343], ["baz@bar.com", 0.183], ["bar@bar.com", 0.157]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["foo@bar.com", 0.233], ["baz@bar.com", 0.262], ["bar@bar.com", 0.251], ["car@bar.com", 0.254]] | |
| ################################################################# | |
| At 2000 elements | |
| ------------------Redis Random----------------------- | |
| [["foo@bar.com", 0.344], ["baz@bar.com", 0.1635], ["car@bar.com", 0.3265], ["bar@bar.com", 0.166]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["foo@bar.com", 0.2435], ["bar@bar.com", 0.249], ["baz@bar.com", 0.271], ["car@bar.com", 0.2365]] | |
| ################################################################# | |
| At 5000 elements | |
| ------------------Redis Random----------------------- | |
| [["foo@bar.com", 0.3308], ["car@bar.com", 0.3352], ["baz@bar.com", 0.17], ["bar@bar.com", 0.164]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["foo@bar.com", 0.2544], ["bar@bar.com", 0.2416], ["baz@bar.com", 0.256], ["car@bar.com", 0.248]] | |
| ################################################################# | |
| At 10000 elements | |
| ------------------Redis Random----------------------- | |
| [["baz@bar.com", 0.1677], ["foo@bar.com", 0.3329], ["car@bar.com", 0.3344], ["bar@bar.com", 0.165]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["foo@bar.com", 0.2541], ["bar@bar.com", 0.2479], ["baz@bar.com", 0.2499], ["car@bar.com", 0.2481]] | |
| ################################################################# | |
| At 20000 elements | |
| ------------------Redis Random----------------------- | |
| [["foo@bar.com", 0.33245], ["bar@bar.com", 0.1707], ["car@bar.com", 0.3304], ["baz@bar.com", 0.16645]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["foo@bar.com", 0.25315], ["car@bar.com", 0.24995], ["bar@bar.com", 0.2473], ["baz@bar.com", 0.2496]] | |
| ################################################################# | |
| At 50000 elements | |
| ------------------Redis Random----------------------- | |
| [["bar@bar.com", 0.1655], ["baz@bar.com", 0.16752], ["car@bar.com", 0.33672], ["foo@bar.com", 0.33026]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["foo@bar.com", 0.24934], ["bar@bar.com", 0.25084], ["car@bar.com", 0.2496], ["baz@bar.com", 0.25022]] | |
| ################################################################# | |
| At 100000 elements | |
| ------------------Redis Random----------------------- | |
| [["car@bar.com", 0.33158], ["baz@bar.com", 0.16556], ["bar@bar.com", 0.16666], ["foo@bar.com", 0.3362]] | |
| ------------------Cycle----------------------- | |
| [["foo@bar.com", 0.25], ["bar@bar.com", 0.25], ["baz@bar.com", 0.25], ["car@bar.com", 0.25]] | |
| ------------------Randomm Ruby----------------------- | |
| [["baz@bar.com", 0.2522], ["bar@bar.com", 0.2504], ["car@bar.com", 0.24905], ["foo@bar.com", 0.24835]] | |
| althea/spaghetti/sf[cycling_email_addresses]% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment