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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:03:14+00:00 2026-05-31T14:03:14+00:00

I am finding something odd happening with my program. This program basically is used

  • 0

I am finding something odd happening with my program.

This program basically is used for component clicking, as it is a concept to test randomness.

enter image description here

As you can see, it is printing correctly, as it should have a tendency to click towards the middle, which is does perfect.

Problem is, it seems biased.

import java.applet.Applet;
import java.awt.Point;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.awt.*;

public class Testing extends Applet {

/**
 * 
 */
private static final long serialVersionUID = -2441995094105327849L;

public void init() {
    setSize(WIDTH, HEIGHT);
    img = createImage(WIDTH, HEIGHT);
    g = img.getGraphics();
    paint(g);
    setVisible(true);
    g.setColor(new Color(0, 0, 0));
    g.fillRect(0, 0, WIDTH, HEIGHT);
    g.setColor(new Color(55, 55, 55, 55));
    main();
}

public final static int WIDTH = 400, HEIGHT = 400;
public static int[] widths = new int[WIDTH], heights = new int[HEIGHT];
public static ArrayList<String> set = new ArrayList<String>();
public Image img;
public Graphics g;

public void paint(Graphics g) {
    g.drawImage(img, 0, 0, null);
}

public void update(Graphics g) {
    paint(g);
}

public void main() {
    int count101 = 0;
    int count100 = 0;
    int count99 = 0;
    try {
        PrintWriter pw = new PrintWriter(new FileWriter(
                new File("Data.dat")));
        Point center = new Point(WIDTH / 2, HEIGHT / 2);

        int runs = 10000000;

        for (int i = 0; i < runs; i++) {
            int x = center.x
                    - (int) ((Math.random() - Math.random())
                            * Math.random() * center.x);
            int y = center.y
                    - (int) ((Math.random() - Math.random())
                            * Math.random() * center.y);
            widths[x]++;
            heights[y]++;
            repaint();
            g.fillRect(x, y, 1, 1);
            if((x & y) == 101){
                count101++;
            }
            if((x & y) == 100){
                count100++;
            }
            if((x & y) == 99){
                count99++;
            }
        }
        System.out.println(count101);
        System.out.println(count100);
        System.out.println(count99);
        repaint();
        pw.flush();
        pw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

This constantly prints biased results.

It prints the following:

3640
10918
3741

This is considerably biased because it follows a trend for the most part, following a linear increase with all the other values, but once it reaches the 100 mark it decides that it is going to drop the bomb and pick it 6% more then every other.

Anyone know any reasoning for this?

Oh, by the way, I do have a txt file containing every result printed out with repetition of 10,000,000 times with percentages etc, it is quite long so I won’t post, but I do have the info.

  • 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-31T14:03:15+00:00Added an answer on May 31, 2026 at 2:03 pm

    It is neither Java Math.rand() nor pseudo random generation problem. This is causing the weird (but expected) behaviour:

    Math.random() - Math.random()
    

    The sum (and subtraction) of two uniformly distributed random variables do not result in uniformly distributed variable. As far as I remember they result in triangular distribution:

    triangular distribution

    See: Distribution of mean of two standard uniform variables.

    This is what you are seeing – a perfect illustration of 2-dimensional random variable with triangular distribution. Moreover, if you keep adding uniformly distributed random variables you will eventually get normal distribution.

    To achieve uniform distribution all you have to do is replace awkward:

    int x = center.x
                    - (int) ((Math.random() - Math.random())
                            * Math.random() * center.x);
    

    with simple:

    int x = (int) (Math.random() * center.x * 2);
    

    Your code (without multiplication) generates random variable with possible values from 0 to center.x * 2 and with expected value at center.x. So far so good. But the distribution is trangular which means the probability density is not equal in that range.

    Multiplication of two random variables (out of which one is no longer uniformly distributed) has even more complex distribution, but certainly not uniform.

    The latter code snippet generates a simple, uniformly distributed variables with probability density function equal in the whole space.

    Side note

    In your program it is an obvious math mistake, but pseudo-random generator can in fact generate “non-random” patterns in space. Have a look at Strange Attractors and TCP/IP Sequence Number Analysis, image from that article:

    3d-space
    (source: coredump.cx)

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

Sidebar

Related Questions

Should I use something like maven for this? Or would finding them as they
I'm interested in finding out why this is used on some Web sites for
I was just finding something on web I come to know that we can
A rather silly Vim question, but I've something to do, and I'm not finding
Finding a good way to do this has stumped me for a while now:
I'm finding that I can't access the admin shares on an XP64 box when
I been finding that for something that I consider pretty import there is very
I would expect something like this to be pretty popular in demand but I
Finding myself in the situation where I have a method with this signature void
Finding lots of mixed answers on this from Google - but mostly 2-3 years

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.