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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:50:38+00:00 2026-06-17T20:50:38+00:00

I made a class called QuickRandom , and its job is to produce random

  • 0

I made a class called QuickRandom, and its job is to produce random numbers quickly. It’s really simple: just take the old value, multiply by a double, and take the decimal part.

Here is my QuickRandom class in its entirety:

public class QuickRandom {
    private double prevNum;
    private double magicNumber;

    public QuickRandom(double seed1, double seed2) {
        if (seed1 >= 1 || seed1 < 0) throw new IllegalArgumentException("Seed 1 must be >= 0 and < 1, not " + seed1);
        prevNum = seed1;
        if (seed2 <= 1 || seed2 > 10) throw new IllegalArgumentException("Seed 2 must be > 1 and <= 10, not " + seed2);
        magicNumber = seed2;
    }

    public QuickRandom() {
        this(Math.random(), Math.random() * 10);
    }

    public double random() {
        return prevNum = (prevNum*magicNumber)%1;
    }

}

And here is the code I wrote to test it:

public static void main(String[] args) {
        QuickRandom qr = new QuickRandom();

        /*for (int i = 0; i < 20; i ++) {
            System.out.println(qr.random());
        }*/

        //Warm up
        for (int i = 0; i < 10000000; i ++) {
            Math.random();
            qr.random();
            System.nanoTime();
        }

        long oldTime;

        oldTime = System.nanoTime();
        for (int i = 0; i < 100000000; i ++) {
            Math.random();
        }
        System.out.println(System.nanoTime() - oldTime);

        oldTime = System.nanoTime();
        for (int i = 0; i < 100000000; i ++) {
            qr.random();
        }
        System.out.println(System.nanoTime() - oldTime);
}

It is a very simple algorithm that simply multiplies the previous double by a “magic number” double. I threw it together pretty quickly, so I could probably make it better, but strangely, it seems to be working fine.

This is sample output of the commented-out lines in the main method:

0.612201846732229
0.5823974655091941
0.31062451498865684
0.8324473610354004
0.5907187526770246
0.38650264675748947
0.5243464344127049
0.7812828761272188
0.12417247811074805
0.1322738256858378
0.20614642573072284
0.8797579436677381
0.022122999476108518
0.2017298328387873
0.8394849894162446
0.6548917685640614
0.971667953190428
0.8602096647696964
0.8438709031160894
0.694884972852229

Hm. Pretty random. In fact, that would work for a random number generator in a game.

Here is sample output of the non-commented out part:

5456313909
1427223941

Wow! It performs almost 4 times faster than Math.random.

I remember reading somewhere that Math.random used System.nanoTime() and tons of crazy modulus and division stuff. Is that really necessary? My algorithm performs a lot faster and it seems pretty random.

I have two questions:

  • Is my algorithm “good enough” (for, say, a game, where really random numbers aren’t too important)?
  • Why does Math.random do so much when it seems just simple multiplication and cutting out the decimal will suffice?
  • 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-17T20:50:39+00:00Added an answer on June 17, 2026 at 8:50 pm

    Your QuickRandom implementation hasn’t really an uniform distribution. The frequencies are generally higher at the lower values while Math.random() has a more uniform distribution. Here’s a SSCCE which shows that:

    package com.stackoverflow.q14491966;
    
    import java.util.Arrays;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            QuickRandom qr = new QuickRandom();
            int[] frequencies = new int[10];
            for (int i = 0; i < 100000; i++) {
                frequencies[(int) (qr.random() * 10)]++;
            }
            printDistribution("QR", frequencies);
    
            frequencies = new int[10];
            for (int i = 0; i < 100000; i++) {
                frequencies[(int) (Math.random() * 10)]++;
            }
            printDistribution("MR", frequencies);
        }
    
        public static void printDistribution(String name, int[] frequencies) {
            System.out.printf("%n%s distribution |8000     |9000     |10000    |11000    |12000%n", name);
            for (int i = 0; i < 10; i++) {
                char[] bar = "                                                  ".toCharArray(); // 50 chars.
                Arrays.fill(bar, 0, Math.max(0, Math.min(50, frequencies[i] / 100 - 80)), '#');
                System.out.printf("0.%dxxx: %6d  :%s%n", i, frequencies[i], new String(bar));
            }
        }
    
    }
    

    The average result looks like this:

    QR distribution |8000     |9000     |10000    |11000    |12000
    0.0xxx:  11376  :#################################                 
    0.1xxx:  11178  :###############################                   
    0.2xxx:  11312  :#################################                 
    0.3xxx:  10809  :############################                      
    0.4xxx:  10242  :######################                            
    0.5xxx:   8860  :########                                          
    0.6xxx:   9004  :##########                                        
    0.7xxx:   8987  :#########                                         
    0.8xxx:   9075  :##########                                        
    0.9xxx:   9157  :###########                                       
    
    MR distribution |8000     |9000     |10000    |11000    |12000
    0.0xxx:  10097  :####################                              
    0.1xxx:   9901  :###################                               
    0.2xxx:  10018  :####################                              
    0.3xxx:   9956  :###################                               
    0.4xxx:   9974  :###################                               
    0.5xxx:  10007  :####################                              
    0.6xxx:  10136  :#####################                             
    0.7xxx:   9937  :###################                               
    0.8xxx:  10029  :####################                              
    0.9xxx:   9945  :###################    
    

    If you repeat the test, you’ll see that the QR distribution varies heavily, depending on the initial seeds, while the MR distribution is stable. Sometimes it reaches the desired uniform distribution, but more than often it doesn’t. Here’s one of the more extreme examples, it’s even beyond the borders of the graph:

    QR distribution |8000     |9000     |10000    |11000    |12000
    0.0xxx:  41788  :##################################################
    0.1xxx:  17495  :##################################################
    0.2xxx:  10285  :######################                            
    0.3xxx:   7273  :                                                  
    0.4xxx:   5643  :                                                  
    0.5xxx:   4608  :                                                  
    0.6xxx:   3907  :                                                  
    0.7xxx:   3350  :                                                  
    0.8xxx:   2999  :                                                  
    0.9xxx:   2652  :                                                  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class called Job with a public ctor and public function
Simple question. I made a class called Tester1 which extends another called Tester2. Tester2
I made a class called SpecializationBean which has two private fields: private ArrayList<SelectItem> specializationItems=
Lets suppose I made a class called Person. var Person = function(fname){this.fname = fname;};
I am writing a program to find adapters, and have made a class called
Trying to import a class I made in Dr. Java. I made a simple
I just wrote some code. It has my own custom made class. In that
I made a class called ImageLabel which extends QLabel. I want it to keep
I have a custom class called SaveData that is made up of four strings.
OK so I have made a class called Courses with private member functions courseName,

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.