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

  • Home
  • SEARCH
  • 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 3945404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:00:46+00:00 2026-05-20T01:00:46+00:00

Which set is more random? Math.random() for Java or random for Mathematica? Java is

  • 0

Which set is more “random”?

Math.random() for Java or random for Mathematica? Java is in blue, Mathematica in red.

numbers are from 0 to 50 (51?) Mathematica random vs Java

EDIT:
It’s a histogram generated in Mathematica.

Java Source (ugly)

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 0;
        int sum = 0;
        int counter = 0;
        String randomNumberList = " ";
        int c = 0;
        while (c != 50){

            while (i != 7) {
            i = (int) (51 * Math.random());
            sum += i;
            ++counter;
            randomNumberList += " " + i;
            }
        i = 0;
        System.out.print("\n" + randomNumberList);
        ++c;
        }

    }

Mathematica source (output.txt is the dump from Java)

dataset = ReadList["~/Desktop/output.txt", Number] 
dataset2 = RandomReal [{0, 50}, 50000]
Histogram[{dataset, dataset2}]

[EDIT]: I was just learning loops when I did the code. Sorry for the confusion. Now I made a cleaner version and they are about equally distributed. I guess that arbitrary loop ending made a big difference.

New mathematica output

new code:

public class RandomNums {

  public static void main(String[] args) {
    int count = 0;
    for (int i = 0; i <= 50000; i++){
        int j = (int) (50 * Math.random());
        System.out.print(j + " ");
        count++;
        if (count == 50){
            System.out.println("\n");
            count = 0;
        }
     }
  }
}
  • 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-20T01:00:47+00:00Added an answer on May 20, 2026 at 1:00 am

    If this plot suggests anything to me, it is that the quality of Mathematica’s uniform random distribution is much better than the implementation in Java you are showing (I don’t claim that for any Java implementation. Also, as a disclaimer, and not to start a flame war, I’ve been both J2EE and Mathematica developer for some time, although admittedly have more experience in the latter).

    Here is the argument. You have 50000 points and 50 bins (histogram bars) shown, which suggests that you roughly have 1000 points per bin. More precisely, we can use ergodicity to cast the problem of 50000 uniformly distributed points into that of 50000 independent trials, and ask what is the mean number of points to end up in each bin, and the variance. The probability that any particular bin ends up with exactly k points out of Npoints is given then by a binomial distribution:

    enter image description here

    For which, the mean is Npoints/Nbins (which is what we expect intuitively, of course), and the variance is Npoints * (1-1/Nbins)* 1/Nbins ~ Npoints/Nbins = 1000, in our case (Npoints = 50000, Nbins = 50). Taking a square root, we get the standard deviation as sqrt(1000) ~ 32, which is about 3% of the mean (which is 1000). The conclusion is that, for an ideal uniform distribution, and for a given number of points and bins, we should expect deviations from the mean of the order of 3%, for each bin. And this is very similar to what Mathematica distribution gives us, judging by the picture. The deviations for individual bins for Java distribution (again, the particular implementation presented here), are much larger, and suggest correlations between bins and that overall this uniform distribution is of much poorer quality.

    Now, this is a “high-level” argument, and I am not going into details to discover the reason. This seems logical however, given that the traditional target audience for Mathematica (sciences, academia) is (or at least used to be) much more demanding in this respect, than that for Java. That said, I have no doubts that there exist many excellent Java implementations of random number generators for many statistical distributions – they are just not something built into the language, unlike in Mathematica.

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

Sidebar

Related Questions

No related questions found

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.