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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:54:11+00:00 2026-06-08T05:54:11+00:00

I have a function that generates random output (string). I need to call that

  • 0

I have a function that generates random output (string).

I need to call that function until I get 3 different outputs (strings).

What is the most elegant way to generate array with 3 unique strings by calling the function, with the limit how many times the function can be called if the output is not generated in specified number of attempts?

Here’s what I currently have:

output = []
limit_calls = 5
limit_calls.times do |i|
  str = generate_output_function
  output.push str
  break if output.uniq.size > 2
end

Can this be beautified / shortened to 1 line? I’m pretty sure in ruby.. 🙂

Thanks

  • 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-06-08T05:54:12+00:00Added an answer on June 8, 2026 at 5:54 am

    Using a set makes it (a bit) easier:

    require 'set'
    output = Set.new
    limit_calls = 5
    call_count = 0
    while output.size < 3 and call_count < limit_calls
      output << generate_output_function 
      call_count += 1
    end
    output
    

    or with an array

    output = []
    limit_calls = 5
    while output.size < limit_calls and output.uniq.size < 3
      output << generate_output_function 
    end
    output.uniq
    

    UPDATE with the call limit. Seems like the Array version wins! Thanks Iain!

    Will also ponder a version using inject.

    UPDATE 2 – with inject:

    5.times.inject([]) { |a, el| a.uniq.size < 3 ? a << generate_output_function : a }
    

    there is your oneliner. I am not sure I prefer it cause it is a bit hard to follow…..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that generates normal random number matrix having normal distribution using
I have a function that generates a random combination. my function looks like this:
I have a program that generates a single random character, using the randomCharacter function,
If I have a PHP function that generates a random number, is it possible
Hi I have a function that will generate a random string which works fine.
I have a function 'generateRan' that generates random numbers. This function can not be
I have a function that generates a key of 4 characters that has to
I'll start off with a solid example: I have a function that generates hashes
I have a function that returns two values in a list. Both values need
This question was asked in my interview. random(0,1) is a function that generates integers

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.