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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:24:13+00:00 2026-06-05T22:24:13+00:00

So basically for fun I was trying to generate a column of numbers (7

  • 0

So basically for fun I was trying to generate a column of numbers (7 digits only 0s and 1s)
My code’s pretty short:

a = rand(0000000-1111111)
b = 220
a1 = rand(0000000-1111111)
a2 = rand(0000000-1111111)
a3 = rand(0000000-1111111)
a4 = rand(0000000-1111111)
a5 = rand(0000000-1111111)

while b !=0
  puts a
  puts a2
  puts a3
  puts a4
  puts a5
end

My problem is that instead of generating a random column of 0s and 1s all the numbers are used.

  • 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-05T22:24:15+00:00Added an answer on June 5, 2026 at 10:24 pm

    Here’s idiomaticish Ruby:

    5.times do
      puts (1..7).map { [0, 1].sample }.join
    end
    

    Let’s unpack it:

    5.times do...end is pretty self-explanatory. Do whatever’s between do and end five times.

    (1..7) generates an array with seven elements. We don’t actually care what’s in it right now. map returns a new array where each element is the result of calling what’s between the braces. So seven times we’ll call [0, 1].sample and squeeze the results into an array. The sample itself, of course, randomly picks either 0 or 1. Finally .join turns an array into a string. If we’d said .join('-'), for example, it’d put a hyphen between each element (1-0-0-1-1-1-0-1). But since we didn’t specify anything it puts nothing between each element (10011101).

    And there you have it.

    As others have noted, for this particular problem it’s possible to do faster and shorter things by using binary. I don’t think this is the Ruby Way though. With respect to speed, “premature optimization is the root of all evil”, and if you have a violent aversion to slow code you shouldn’t be coding Ruby anyway. With regards to readability, that way may be shorter, but the above way is a lot clearer. “Oh, we’re doing something five times, and that’s going to be printing out a 7-thing long…random sequence of 0s and 1s…as a string”. It almost reads like English (if you know the word map (definition three)).

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

Sidebar

Related Questions

basically i have (state, state code) pairs, that are subsets of country [USA] ->
For a fun project I'm trying to implement the BitTorrent spec, and right now
I am having fun with google maps and was after some assistance. Basically I
The following code: let CreateFunc= let counter = ref 0 fun () -> counter
I was fiddling around with Python's generators and iterable class, just for fun. Basically
I'm trying to make a jquery plugin (just for fun) and I can't seem
I thought it would be fun if I could write vb.net or c# code
I'm trying to create an as.data.frame.by method which basically melts the N-dimensional by object
I'm having a littl fun with ASP.Net data binding today, basically I have two
I'm pretty new to python and am trying to grab the ropes and decided

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.