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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:42:53+00:00 2026-06-17T17:42:53+00:00

I am in need of an algorithm (in Java, but the theory should be

  • 0

I am in need of an algorithm (in Java, but the theory should be pretty general) for some sort of probability… thing. I don’t even know what to call it, which is why I haven’t had any luck Googling.

To describe a bit better, I have a task that needs to be done X number of times when a function is called. Sometimes, there’s only a 1 in 10 chance of that happening, meaning that (approximately) one out of every ten times that function is called that task will actually be performed – easy enough to do with random(). Sometimes it will be 2 in 10, or maybe even 10 in 10. Still easy enough, straightforward conditional, even if it’s not always “X in 10”

The problem is, it can have greater than a 1 in 1 chance. It may be 15 in 10 – half the time it runs once, the rest it runs twice. Or 10 in 5, where it runs twice (approximately) every time called. As you can see, this has now ventured out of simple inequality testing.

So, what I’m looking for is some algorithm that, based on random numbers, will return a run count based on how frequently (1 in 10, 200%, 0.75, whatever format works) something ought to happen. If anyone can provide any leads on this – like, maybe an actual term to search for – it’d be much appreciated!

EDIT: No wonder I can’t find many results, everyone’s as confused as I am!

First and foremost, no real “maximum” value. Not formally defined. If the algorithm can extract that from the probability value it receives, that’s great.

It also definitely needs to be random, making it inherently imperfect. If you flip a coin 10 times, you may very well end up with 8 heads, even though it theoretically ought to be perfectly even! And that’s alright. In fact, it’s the point.

I could tell you why I need it… but that would be in violation of proper object-oriented modularity practices 🙂 External parties need only know the interface; it accepts a value (probably a float, “0.75” seems like it would work best) and returns an int. If you call it 100 times, with a probability of 1 in 5, the sum of its returned values should average out to 20.

  • 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-17T17:42:54+00:00Added an answer on June 17, 2026 at 5:42 pm

    If the number of executions for a given probability X is to be limited to floor(X) and ceil(X) (and never floor(X)-1 or less or ceil(X)+1 or more): (so 1.5 is 50% 1 and 50% 2 and 0.7 is 70% 1 and 30% 0)

    int runCount = (int)probability;
    if (randomGen.nextDouble() < probability - runCount)
      runCount++;
    

    EDIT: Condensed as per DigitalMan’s suggestion.

    EDIT 2: Admittedly, this solution focusses on the “15 in 10 – half the time it runs once, the rest it runs twice” in the question and can be thought of as replacing the “approximately” in “10 in 5, where it runs twice (approximately) every time called” in the question with “exactly”. The constraints are somewhat unclear.

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

Sidebar

Related Questions

I almost don't know C++, but I need help to solve project build problem.
I am in need of an algorithm (preferably Java) that can find the combination
I have a large text file I need to sort in Java. The format
In Java, I need an algorithm to find the max. number of occurrences in
I have an application in Java/JSF, I need to do some optimization calculations, like
I wrote a java program that should fill a sqlite database with some rows
I'm trying to translate a radix sort algorithm from Java to Haskell, and I'm
I'm coding a flocking algorithm in Java but I'm stuck at a certain point
I need an algorithm which given a list L and a number N ,
I need the algorithm to animate the arrow based on 2 parameters, angle while

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.