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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:43:16+00:00 2026-06-14T03:43:16+00:00

Possible Duplicate: True random generation in Java Java random always returns the same number

  • 0

Possible Duplicate:
True random generation in Java
Java random always returns the same number when I set the seed?

I run this piece of code in one of my programs.

public String[] gen_phase_zero() {
        Random generator = new Random();
        int r = generator.nextInt(2);
        if (r == 1) {
            String big = "A";
            String small = "a";
            return new String[] { big, small };
        } else {
            String big = "B";
            String small = "b";
            return new String[] { big, small };
        }
    }

If I run this a few times my output is like this.

Aa
Aa
Aa
Aa
Bb
Aa
Aa
Aa
Bb

It’s not alwasy in that order. But it’s almost never anything close to 50/50

Update:

I’m not expeccting fifty fifty, but it seems that if “Aa” is selected first, then it’ll be next around 3 more times, but if Bb is selected first, it’ll be selected the next three times as well.

  • 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-14T03:43:17+00:00Added an answer on June 14, 2026 at 3:43 am

    Well it doesn’t look too bad to me. Let’s create a more statistically significant test:

    import java.util.Random;
    
    public class Test {
        public static void main(String[] args) throws Exception {
    
            Random rng = new Random();
            int total = 0;
            for (int i = 0; i < 1000000; i++) {
                total += rng.nextInt(2);
            }
            System.out.println("Total: " + total);
        }
    }
    

    Sample output from 5 runs:

    Total: 501184
    Total: 499740
    Total: 500116
    Total: 500374
    Total: 500413
    

    Doesn’t look hugely biased to me…

    I got the same results when calling new Random() inside the loop instead of just once, too – although it’s not a good idea to do that.

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

Sidebar

Related Questions

Possible Duplicate: Java random always returns the same number when I set the seed?
Possible Duplicate: True random number generator I have worked with random functions in python,ruby,
Possible Duplicate: Howto change what Default(T) returns in C# print(default(int) == 0) //true Similarly
Possible Duplicate: Why boolean in Java takes only true or false? Why not 1
Possible Duplicate: In C arrays why is this true? a[5] == 5[a] Is this
Possible Duplicate: In C arrays why is this true? a[5] == 5[a] Is the
Possible Duplicate: In C arrays why is this true? a[5] == 5[a] C weird
Possible Duplicates: Understanding “randomness” Fastest implementation of a true random number generator in C#
Possible Duplicate: In C arrays why is this true? a[5] == 5[a] Consider the
Possible Duplicate: Why does “abcd”.StartsWith(“”) return true? Something similar to this being true caught

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.