random_fu 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +23 -0
- data/lib/random_fu/time.rb +5 -1
- data/lib/random_fu/version.rb +1 -1
- data/spec/time_random_spec.rb +15 -0
- data/spec/version_spec.rb +1 -1
- metadata +1 -1
    
        data/README.md
    CHANGED
    
    | @@ -75,6 +75,29 @@ Returns the value from a random key.  If called on an empty hash, returns nil. | |
| 75 75 | 
             
                {}.random
         | 
| 76 76 | 
             
                # => nil
         | 
| 77 77 |  | 
| 78 | 
            +
            ## Time
         | 
| 79 | 
            +
            --------------
         | 
| 80 | 
            +
            ## Time#random_between(Time)
         | 
| 81 | 
            +
            Returns a Time object which is random between self and the Time object passed in. 
         | 
| 82 | 
            +
            Note, the Time returned MAY be equal to self or the Time passed in!  Lower and 
         | 
| 83 | 
            +
            Upper bound times are possible values, just like 0 and 1 are between?(0,1) in Ruby.
         | 
| 84 | 
            +
             | 
| 85 | 
            +
            ### Examples:
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                t=Time.now
         | 
| 88 | 
            +
                # => 2012-06-27 13:57:02 -0500
         | 
| 89 | 
            +
                t2 = t + 60
         | 
| 90 | 
            +
                # => 2012-06-27 13:58:02 -0500
         | 
| 91 | 
            +
                t.random_between(t2)
         | 
| 92 | 
            +
                # => 2012-06-27 13:57:17 -0500
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                t=Time.new(2012,6,1)
         | 
| 95 | 
            +
                # => 2012-06-01 00:00:00 -0500
         | 
| 96 | 
            +
                t2=Time.new(2012,6,30)
         | 
| 97 | 
            +
                # => 2012-06-30 00:00:00 -0500
         | 
| 98 | 
            +
                t.random_between(t2)
         | 
| 99 | 
            +
                # => 2012-06-09 20:01:45 -0500
         | 
| 100 | 
            +
             | 
| 78 101 | 
             
            License
         | 
| 79 102 | 
             
            =======
         | 
| 80 103 |  | 
    
        data/lib/random_fu/time.rb
    CHANGED
    
    | @@ -16,8 +16,12 @@ module RandomFu | |
| 16 16 | 
             
                  return self if self==other_time
         | 
| 17 17 |  | 
| 18 18 | 
             
                  # for large spans between time, to_i is not efficeint
         | 
| 19 | 
            +
                  rnd = rand(self.to_i - other_time.to_i + 1) 
         | 
| 20 | 
            +
                  # w/o +1, time may equal self but would never equal other_time
         | 
| 21 | 
            +
                  # e.g. .30 seconds and .40 seconds, requires a random number
         | 
| 22 | 
            +
                  # between 0 and 10, thus rand(11), or rand(40-30+1)
         | 
| 23 | 
            +
             | 
| 19 24 | 
             
                  time = self.dup
         | 
| 20 | 
            -
                  rnd = rand(self.to_i - other_time.to_i)
         | 
| 21 25 | 
             
                  time = self.dup + ((self < other_time) ? rnd : -rnd)
         | 
| 22 26 | 
             
                end
         | 
| 23 27 |  | 
    
        data/lib/random_fu/version.rb
    CHANGED
    
    
    
        data/spec/time_random_spec.rb
    CHANGED
    
    | @@ -49,4 +49,19 @@ describe "random_fu's Time#random_between" do | |
| 49 49 | 
             
                end
         | 
| 50 50 | 
             
              end
         | 
| 51 51 |  | 
| 52 | 
            +
              it "may include lower and upper bound" do
         | 
| 53 | 
            +
                # Need to look into srand for deterministic random values
         | 
| 54 | 
            +
                # for deterministic tests...This is ugly and could fail
         | 
| 55 | 
            +
                # if odds are just right
         | 
| 56 | 
            +
                lower_hit = upper_hit = false
         | 
| 57 | 
            +
                10.times do 
         | 
| 58 | 
            +
                  time_ohter = time_now + 1
         | 
| 59 | 
            +
                  rnd = time_now.random_between(time_ohter)
         | 
| 60 | 
            +
                  lower_hit = lower_hit || (rnd.to_i == time_now.to_i)
         | 
| 61 | 
            +
                  upper_hit = upper_hit || (rnd.to_i == time_ohter.to_i)
         | 
| 62 | 
            +
                end
         | 
| 63 | 
            +
                lower_hit.should be_true
         | 
| 64 | 
            +
                upper_hit.should be_true
         | 
| 65 | 
            +
              end
         | 
| 66 | 
            +
             | 
| 52 67 | 
             
            end
         | 
    
        data/spec/version_spec.rb
    CHANGED