<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>test/fixtures/fr_users.yml</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -9,13 +9,20 @@ module GreenRiver #:nodoc:
       # Item.random(5, :conditions =&gt; ...)
       def random(*args)
         limit = args.first
-        options = args.last.is_a?(::Hash) ? pop : {}
-        options[:select] = &quot;id&quot;
-        options.delete(:limit)
-        options.delete(:order)
-        sql = construct_finder_sql(options)
-        all_ids = connection.select_all(sql, &quot;#{name} Load&quot;).collect!{|r| r['id']} 
-        find(Array.new(limit){|i| all_ids.delete_at(rand(all_ids.length - 1))})
+        options = args.last.is_a?(::Hash) ? args.pop : {}
+        # if only one row is requested, use the strategy of getting the size of the
+        # result set, and then in a second query offset by a random int of that
+        if limit == 1
+          result_size = count(options)
+          find(:first, options.merge(:offset =&gt; rand(result_size - 1)))
+        else
+          options[:select] = &quot;id&quot;
+          options.delete(:limit)
+          options.delete(:order)
+          sql = construct_finder_sql(options)
+          all_ids = connection.select_all(sql, &quot;#{name} Load&quot;).collect!{|r| r['id']} 
+          find(Array.new(limit){|i| all_ids.delete_at(rand(all_ids.length - 1))})
+        end
       end
     end
   end</diff>
      <filename>lib/green_river/find_random.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,20 +1,29 @@
 require 'test_helper'
 
-class User &lt; ActiveRecord::Base
+class FrUser &lt; ActiveRecord::Base
 end
 
 class FindRandomTest &lt; Test::Unit::TestCase
-  fixtures :users
+  fixtures :fr_users
   
   def test_size_of_found_collection
-    assert_equal 4, User.random(4).size
+    assert_equal 4, FrUser.random(4).size
   end
 
   def test_found_collection_random
     srand(14)
-    rand_users = User.random(3)
+    rand_users = FrUser.random(3)
     assert_equal 'matilda', rand_users[0].login
     assert_equal 'silvia', rand_users[1].login
     assert_equal 'leon', rand_users[2].login
   end
+
+  def test_finding_single_random_row
+    srand(8)
+    assert_equal 'patricia', FrUser.random(1).login
+  end
+  def test_finding_single_random_row_with_conditions
+    srand(9)
+    assert_equal 'matilda', FrUser.random(1, :conditions =&gt; 'login LIKE &quot;%il%&quot;').login
+  end
 end</diff>
      <filename>test/find_random_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,5 +1,5 @@
 ActiveRecord::Schema.define(:version =&gt; 1) do
-  create_table &quot;users&quot;, :force =&gt; true do |t|
+  create_table &quot;fr_users&quot;, :force =&gt; true do |t|
       t.column &quot;login&quot;,  :string
   end
 end</diff>
      <filename>test/schema.rb</filename>
    </modified>
  </modified>
  <removed type="array">
    <removed>
      <filename>test/fixtures/users.yml</filename>
    </removed>
  </removed>
  <parents type="array">
    <parent>
      <id>66a2c4901f8129f7a25c7242c24acea8318ced64</id>
    </parent>
  </parents>
  <author>
    <name>Ben Tucker</name>
    <email>ben@btucker.net</email>
  </author>
  <url>http://github.com/btucker/find_random/commit/c437d95bcf0fe9fbbaad714fae92e5397b1f1d21</url>
  <id>c437d95bcf0fe9fbbaad714fae92e5397b1f1d21</id>
  <committed-date>2008-03-03T14:52:47-08:00</committed-date>
  <authored-date>2008-03-03T14:52:47-08:00</authored-date>
  <message>use more effective strategy when only one result requested</message>
  <tree>54e35d8f69d50b563cadef33d3bdf5953abec7bb</tree>
  <committer>
    <name>Ben Tucker</name>
    <email>ben@btucker.net</email>
  </committer>
</commit>
