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

  • SEARCH
  • Home
  • 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 985173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:07:38+00:00 2026-05-16T05:07:38+00:00

What is the easiest way to return an array in random order in Ruby?

  • 0

What is the easiest way to return an array in random order in Ruby?
Anything that is nice and short that can be used in an IRB session like

[1,2,3,4,5].random()
# or 
random_sort([1,2,3,4,5])
  • 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-16T05:07:38+00:00Added an answer on May 16, 2026 at 5:07 am

    If you don’t have [].shuffle, [].sort_by{rand} works as pointed out by sepp2k. .sort_by temporarily replaces each element by something for the purpose of sorting, in this case, a random number.

    [].sort{rand-0.5} however, won’t properly shuffle. Some languages (e.g. some Javascript implementations) don’t properly shuffle arrays if you do a random sort on the array, with sometimes rather public consequences.

    JS Analysis (with graphs!): http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html

    Ruby is no different! It has the same problem. 🙂

    #sort a bunch of small arrays by rand-0.5
    a=[]
    100000.times{a <<  [0,1,2,3,4].sort{rand-0.5}}
    
    #count how many times each number occurs in each position
    b=[]
    a.each do |x|
        x.each_index do |i|
            b[i] ||=[]
            b[i][x[i]] ||= 0
            b[i][x[i]] += 1
        end
    end
    p b
    

    =>

    [[22336, 18872, 14814, 21645, 22333],
     [17827, 25005, 20418, 18932, 17818],
     [19665, 15726, 29575, 15522, 19512],
     [18075, 18785, 20283, 24931, 17926],
     [22097, 21612, 14910, 18970, 22411]]
    

    Each element should occur in each position about 20000 times. [].sort_by(rand) gives much better results.

    #sort with elements first mapped to random numbers
    a=[]
    100000.times{a <<  [0,1,2,3,4].sort_by{rand}}
    
    #count how many times each number occurs in each position
    ...
    

    =>

    [[19913, 20074, 20148, 19974, 19891],
     [19975, 19918, 20024, 20030, 20053],
     [20028, 20061, 19914, 20088, 19909],
     [20099, 19882, 19871, 19965, 20183],
     [19985, 20065, 20043, 19943, 19964]]
    

    Similarly for [].shuffle (which is probably fastest)

    [[20011, 19881, 20222, 19961, 19925],
     [19966, 20199, 20015, 19880, 19940],
     [20062, 19894, 20065, 19965, 20014],
     [19970, 20064, 19851, 20043, 20072],
     [19991, 19962, 19847, 20151, 20049]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby, given an array of elements, what is the easiest way to return
I have a Spring controller that returns XHTML, what's the easiest way to setup
What would be the easiest way to convert the following key->value object array to
I just wondered what's the easiest way to replace a string characters that must
Whats the easiest and quickest way to loop through an array of numbers and
What would be the easiest way to detach a specific JPA Entity Bean that
What is the easiest way to set up an SMS alerting system so that
What is the preferred/easiest way to find the control that is currently receiving user
What is the easiest way to retrieve a bean id from inside that bean
Possible Duplicate: Easiest way to find duplicate values in a javascript array How do

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.