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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:28:11+00:00 2026-05-22T01:28:11+00:00

Nothing too complicated, basically I just want to pick an element from the array

  • 0

Nothing too complicated, basically I just want to pick an element from the array as if I were making coin tosses for each index and and choosing the index when I first get a head. Also no heads means I choose the last bin.

I came up with the following and was wondering if there was a better/more efficient way of doing this.

def coin_toss(size)
  random_number = rand(2**size)
  if random_number == 0
    return size-1
  else
    return (0..size-1).detect { |n| random_number[n] == 1 }
  end
end
  • 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-22T01:28:12+00:00Added an answer on May 22, 2026 at 1:28 am

    First guess…pick a random number between 1 and 2**size, find the log base 2 of that, and pick the number that many elements from the end.

    Forgive my horrible ruby skillz.

    return a[-((Math.log(rand(2**size-1)+1) / Math.log(2)).floor) - 1]
    

    if rand returns 0, the last element should be chosen. 1 or 2, the next to last. 3, 4, 5, or 6, third from the end. Etc. Assuming an even distribution of random numbers, each element has twice as much chance of being picked as the one after it.

    Edit: Actually, it seems there’s a log2 function, so we don’t have to do the log/log(2) thing.

    return a[-(Math.log2(rand(2**size - 1)+1).floor) - 1]
    

    You may be able to get rid of those log calls altogether like

    return a[-((rand(2**size-1)+1).to_s(2).length)]
    

    But you’re creating an extra String. Not sure whether that’s better than complicated math. 🙂

    Edit: Actually, if you’re going to go the string route, you can get rid of the +1 and -1 altogether. It’d make the probabilities more accurate, as the last two elements should have an equal chance of being chosen. (If the next-to-last value isn’t chosen, the last value would always be.)

    Edit: We could also turn the ** into a bit shift, which should be a little faster (unless Ruby was smart enough to do that already).

    return a[-(rand(1<<size).to_s(2).length)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a simple DSL in C#. Nothing too complicated. I'm looking
I want to create a simple Mario-like 2D game. Nothing too fancy, just a
I want to add a menu to my First Ever Rails Application. Nothing too
The following code hides a form for 10 seconds. Nothing too crazy. Each time
I'm currently designing a program that will involve some physics (nothing too fancy, a
Could any one give an explanation on how a DHT works? Nothing too heavy,
I know almost nothing about linq. I'm doing this: var apps = from app
I'm using CodeIgniter and need an easy to implement captcha. Something simple, nothing too
Nothing too exotic. I have classes in a library project, which is set to
I'm currently in the process of creating a three row layout, nothing too difficult

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.