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 6638787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:28:40+00:00 2026-05-25T23:28:40+00:00

I am struggling to write a clean method which when passed an array of

  • 0

I am struggling to write a clean method which when passed an array of strings and x returns a randomised list of array elements totalling x, eg.

def getrandomarrayelements(thearray, howmany)
    return [something]
end

Yes I should submit my existing code, which whilst works is not good, it’s 8 lines long and I have a feeling it can be done in one?!

  • 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-25T23:28:41+00:00Added an answer on May 25, 2026 at 11:28 pm

    In ruby 1.9:

    irb(main):001:0> [1,2,3,4,5].sample(3)
    => [2, 4, 5]
    irb(main):002:0> [1,2,3,4,5].sample(3)
    => [2, 5, 3]
    

    and for ruby 1.8 something like this:

    def sample(arr, n)
      arr.shuffle[0...n]
    end
    
    irb(main):009:0> sample([1,2,3,4,5], 3)
    => [5, 1, 3]
    irb(main):010:0> sample([1,2,3,4,5], 3)
    => [3, 4, 2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am struggling to write a batch script which can read a CSV file
I am trying to write a method for a Compact Framework 2.0 dll which
I'm struggling to write a xpath which will help me finding image for popup.gif
I'm struggling with PL/SQL functions. I'm trying to write a function which would take
We currently got 100 staging tables which are loaded everyday. I'm struggling to write
I have been struggling to write a simple code to sort an array of
I'm struggling to write a Windows Service that accesses a website, logs in using
I am struggling to write a query to result in the following records. I
Iam struggling with NHibernate and its lazyload. I have a structure which I simplified
Hi i'm struggling to write a particular MySQL Join Query. I have a table

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.