Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6547327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:49:04+00:00 2026-05-25T11:49:04+00:00

This seems like an incredibly simple problem however it isn’t working out as trivially

  • 0

This seems like an incredibly simple problem however it isn’t working out as trivially as I’d expected.

I have a club which has club members and I’d like to pull out two members at random from a club.

Using RANDOM()

One way is to use random ordering:

club.members.find(:all, :order => 'RANDOM()').limit(2)

However that is different for SqLite (the dev database) and Postgres (production) since in MySql the command is RAND().

While I could start writing some wrappers around this I feel that the fact that it hasn’t been done already and doesn’t seem to be part of ActiveRecord tells me something and that RANDOM may not be the right way to go.

Pulling items out directly using their index

Another way of doing this is to pull the set in order but then select random records from it:

First off we need to generate a sequence of two unique indices corresponding to the members:

all_indices = 1..club.members.count
two_rand_indices = all_indices.to_a.shuffle.slice(0,2)

This gives an array with two indices guaranteed to be unique and random. We can use these indices to pull out our records

@user1, @user2 = Club.members.values_at(*two_rand_indices)

What’s the best method?

While the second method is seems pretty nice, I also feel like I might be missing something and might have over complicated a simple problem. I’m clearly not the first person to have tackled this so what is the best, most SQL efficient route through it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:49:05+00:00Added an answer on May 25, 2026 at 11:49 am

    The problem with your first method is that it sorts the whole table by an unindexable expression, just to take two rows. This does not scale well.

    The problem with your second method is similar, if you have 109 rows in your table, then you will generate a large array from to_a. That will take a lot of memory and time to shuffle it.

    Also by using values_at aren’t you assuming that there’s a row for every primary key value from 1 to count, with no gaps? You shouldn’t assume that.

    What I’d recommend instead is:

    1. Count the rows in the table.

      c = Club.members.count
      
    2. Pick two random numbers between 1 and the count.

      r_a = 2.times.map{ 1+Random.rand(c) }
      
    3. Query your table with limit and offset.
      Don’t use ORDER BY, just rely on the RDBMS’s arbitrary ordering.

      for r in r_a
          row = Club.members.limit(1).offset(r)
      end
      

    See also:

    • How can i optimize MySQL’s ORDER BY RAND() function?
    • Quick selection of a random row from a large table in MySQL
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a very simple and a very common problem. The simplest example
This sure seems like a simple error to fix. However, I somehow can't figure
This seems like a pretty softball question, but I always have a hard time
This seems like a simple question, but I can't find it with the Stack
This seems like such a trivial problem, but I can't seem to pin how
This seems like such a simple question, but I'm having such difficulty with it.
this seems like very simple maths but somehow, my brain cant think ... i
this seems like a really simple Question...but behold :) Geek name:string Laser geek_id:integer, power:integer
This seems like a silly question but I would really like your comments and
This seems like perhaps a naive question, but I got into a discussion with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.