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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:34:47+00:00 2026-06-02T19:34:47+00:00

What might be a simple Ruby way to round numbers using probability, i.e., based

  • 0

What might be a simple Ruby way to round numbers using probability, i.e., based on how close the value is to one boundary or the other (floor or ceiling)?

For example, given a current price value of 28.33, I need to add 0.014.

Equivalent to starting with 28.34 and needing to add 0.004, but the final value must be rounded to two decimal places(which can be provided as parameter, or fixed for now).

The final value should therefore be:

  • 28.34 with 60% chance, since it is that much closer, OR
  • 28.35 with 40% random chance

The reason it occured to me this could serve best is that the application is stateless and independent across runs, but still needs to approximate the net effect of accumulating the less significant digits normally rounded into oblivion (eg. micropenny values that do have an impact over time). For example, reducing a stop-loss by some variable increment every day (subtraction like -0.014 above instead).

It would be useful to extend this method to the Float class directly.

  • 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-02T19:34:49+00:00Added an answer on June 2, 2026 at 7:34 pm

    How about:

    rand(lower..upper) < current ? lower.round(2) : upper.round(2)
    

    EDIT:

    The above will only work if you use Ruby 1.9.3 (due to earlier versions not supporting rand in a range).

    Else

    random_number = rand * (upper-lower) + lower
    random_number < current ? lower.round(2) : upper.round(2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be simple or impossible ;-) Is there a way to restrict the
This might be simple but I am new to Oracle. I am using Oracle
this might be fairly simple but I cant find my way around... I have
This might be a simple one, but since I don't have much knowledge about
I'm building a simple JSON API using Rails 3.2.1 and Jbuilder on Ruby 1.8.7
This might be a simple question, but i can't seem to grasp it. I
This might be a simple answer, but I am having some issues writing this
This might be a simple question but I'm just picking this up so please
This might seem ridiculously simple, but I've been getting all kinds of error depending
This might be a simple question for some people since it was quite hard

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.