The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 3 of 3

Thread: [JAVA] random number generator is repeating?

  1. #1

    [JAVA] random number generator is repeating?

    How often will Java's random number generator repeat its sequence of random numbers? Here, I'm shooting 5,000 random photons out of a light source for indirect lighting using only one Random object. It looks like there are only a few dozen distinct places where the photons have landed.

  2. #2
    Well, probably not at all if you have a large enough range. Take this code for example:
    import java.util.*;
    public class Main {
    	public static void main(String args[]){
    		Random gen = new Random();
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		for(int i = 0; i < 5000; i++){
    		ArrayList<Integer> subsets = new ArrayList<Integer>();
    		for(int i = 2; i < 5000; i++){
    			subsets.add(numSubsets(list, i));
    		System.out.println("Top 10 most frequent subset sizes:");
    		for(int i = 4988; i < 5000 - 2; i++){
    	public static int numSubsets(ArrayList<Integer> array, int size){
    		ArrayList<Integer> baseSubset = new ArrayList<Integer>();
    		for(int i = 0; i < size; i++){
    		ArrayList<Integer> currSubset = (ArrayList<Integer>) baseSubset.clone();
    		int count = 0;
    		for(int i = 0; i < array.size() - size - 2; i++){
    			currSubset.add(array.get(i + size));
    			boolean success = true;
    			for(int j = 0; j < currSubset.size(); j++){
    				if(baseSubset.get(j) != currSubset.get(j)){
    					success = false;
    			if(success) count++;
    		return count;
    If you change gen.nextInt() to gen.nextInt(3), or something, then you will probably notice that you get many more repeating subsets, since you are only dealing with a few integers as opposed to INT_MAX_VALUE, or whatever it's called in Java.

    If you still feel that your random values are off, you could try using the Quantum Random Bit Generator Service.

  3. #3
    Ok. So I think there is nothing wrong with the random number generation now. It was just that I was shooting the photons too randomly. Here I've hardcoded the Y direction of the photon to be -0.5 and chose random values for X and Y. Now most of the photons are directed toward my scene rather than going off in space.

    PHP Code:
    public Photon(Light lightRandom rand)
    this.color Colors.multiply(light.colorlight.getBrightness());
    this.ray = new Ray(
    rand.nextDouble() - 0.5,
    rand.nextDouble() - 0.5

    The top shows direct illumination. The bottom show indirect (bouncing off of something once).

    As you can see, the light forms a cone shape. I don't really want that since it is a point light source. What's the correct math to randomly shoot photons over an entire hemisphere rather than a cone?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012