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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:58:14+00:00 2026-06-07T22:58:14+00:00

Okay, I implemented this SO question to my code: Return True or False Randomly

  • 0

Okay, I implemented this SO question to my code: Return True or False Randomly

But, I have strange behavior: I need to run ten instances simultaneously, where every instance returns true or false just once per run. And surprisingly, no matter what I do, every time i get just false

Is there something to improve the method so I can have at least roughly 50% chance to get true?


To make it more understandable: I have my application builded to JAR file which is then run via batch command

 java -jar my-program.jar
 pause

Content of the program – to make it as simple as possible:

public class myProgram{

    public static boolean getRandomBoolean() {
        return Math.random() < 0.5;
        // I tried another approaches here, still the same result
    }

    public static void main(String[] args) {
        System.out.println(getRandomBoolean());  
    }
}

If I open 10 command lines and run it, I get false as result every time…

  • 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-07T22:58:16+00:00Added an answer on June 7, 2026 at 10:58 pm

    I recommend using Random.nextBoolean()

    That being said, Math.random() < 0.5 as you have used works too. Here’s the behavior on my machine:

    $ cat myProgram.java 
    public class myProgram{
    
       public static boolean getRandomBoolean() {
           return Math.random() < 0.5;
           //I tried another approaches here, still the same result
       }
    
       public static void main(String[] args) {
           System.out.println(getRandomBoolean());  
       }
    }
    
    $ javac myProgram.java
    $ java myProgram ; java myProgram; java myProgram; java myProgram
    true
    false
    false
    true
    

    Needless to say, there are no guarantees for getting different values each time. In your case however, I suspect that

    A) you’re not working with the code you think you are, (like editing the wrong file)

    B) you havn’t compiled your different attempts when testing, or

    C) you’re working with some non-standard broken implementation.

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

Sidebar

Related Questions

Okay this question is very simple: I have a facebook page, and a website.
Okay, this is probably a very basic question; but, I'm just getting back in
Okay, I have yet another Code Contracts question. I have a contract on an
Okay, this is a simple question, but I'd like some oppinions on the correct
Okay this is a bit of a noobish question, but I ran across this
Okay..so before you mark this is as a repeated question, read it. I've implemented
Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay, I feel a bit foolish for having to ask this but I guess
Okay, the question might seem dumb, but I'm asking it anyways. After struggling for
I have implement gmail oAuth in my application, everything works okay. question is how

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.