Hello there. It's me again.

I think I got most of the Pseudorandom code fixed but there's a problem.

// Name: Wesley Montgomery
// Assignment 1 (Pseudorandom Class)
// CS 211 (5:30 PM-7:40 PM)
// Craig Niiyama
// This class creates a Pseudorandom Number as well as returns the number divided by the
// Modulus and creates a list displaying the Number of occurrences between 0.0 to 1.0


public class pseudorandom {
	    private int multi; //The Multiplier
	    private int seed;  //The Seed
	    private int incr;  //The Increment
	    private int modu;  //The Modulus
	    private int answer; //The Answer
	    private double divmod; // The Number gotten by dividing the answer with the Modulus.
	    


	   
	   
	    public pseudorandom (int m, int s, int i, int mod){
	    multi = m;
	    seed = s;
	    incr = i;
	    modu = mod;
	    
	    }	
	    
	    public int GenerateNext() //generates the next Seed
	    {
	    	answer=((multi* seed + incr) % modu);
	    	seed=answer;
	    	return answer;	
	    }
	    
	    public double DivideModulus()
	    {
	    	divmod=GenerateNext() / (double) modu;
	    	return divmod;
	    }
	    
	    public static void main (String[] args)
	    {
	    	pseudorandom r= new pseudorandom (40, 36, 3641, 729);
	    	int [] count = new int []{0,0,0,0,0,0,0,0,0,0};
	    	for (int i=0; i < 1000000; i++){
	    	   	int answer=r.GenerateNext();
	    	   	double divmod=r.DivideModulus();
	    	   	//Which range is our value in?
	    	   	if (divmod >= 0.0 || divmod < 0.1)
	    	   	{
	    	   	 count [0] ++;
	    	   	}
	    	   	else if (divmod >= 0.1 || divmod < 0.2)
	    	   	{
	    	   		count [1] ++;
	    	   	}
	    	   	else if (divmod >= 0.2 || divmod < 0.3)
	    	   	{
	    	   		count [2] ++;
	    	   	}
	    	   	else if (divmod >= 0.3 || divmod < 0.4)
	    	   	{
	    	   		count [3] ++;
	    	   	}
	    	   	else if (divmod >= 0.4 || divmod < 0.5)
	    	   	{
	    	   		count [4] ++;
	    	   	}
	    	   	else if (divmod >= 0.5 || divmod < 0.6)
	    	   	{
	    	   		count [5] ++;
	    	   	}
	    	   	else if (divmod >= 0.6 || divmod < 0.7)
	    	   	{
	    	   		count [6] ++;
	    	   	}
	    	   	else if (divmod >= 0.7 || divmod < 0.8)
	    	   	{
	    	   		count [7] ++;
	    	   	}
	    	   	else if (divmod >= 0.8 || divmod < 0.9)
	    	   	{
	    	   		count [8] ++;
	    	   	}
	    	   	else if (divmod >= 0.9 || divmod < 1.0)
	    	   	{
	    	   		count [9] ++;
	    	   	}

		    	//System.out.println(answer);
		    	//System.out.println(divmod);
		    	
	    	}
	    	
	        System.out.println("Range        # of Occurances");
	        System.out.println("[0.0..0.1)       " + count [0]);
	        System.out.println("[0.1..0.2)       " + count [1]);
	        System.out.println("[0.2..0.3)       " + count [2]);
	        System.out.println("[0.3..0.4)       " + count [3]);
	        System.out.println("[0.4..0.5)       " + count [4]);
	        System.out.println("[0.5..0.6)       " + count [5]);
	        System.out.println("[0.6..0.7)       " + count [6]);
	        System.out.println("[0.7..0.8)       " + count [7]);
	        System.out.println("[0.8..0.9)       " + count [8]);
	        System.out.println("[0.9..1.0)       " + count [9]);
	    	
	    }




	}

The List is supposed to list how many times the number appears in a range but the output I get is this.
Range # of Occurances
[0.0..0.1) 1000000
[0.1..0.2) 0
[0.2..0.3) 0
[0.3..0.4) 0
[0.4..0.5) 0
[0.5..0.6) 0
[0.6..0.7) 0
[0.7..0.8) 0
[0.8..0.9) 0
[0.9..1.0) 0

all one million occurrences appear in one range instead of a mixture of different numbers in the different ranges. Can you help me?

Dani AI

Generated

Two brief, concrete points that clarify what went wrong and how to make the routine robust.

The logical bug that pointed out (using || instead of &&) makes the first test true for every real value: any number >= 0.0 satisfies the left side and any number < 0.0 satisfies the right side (which is also < 0.1), so the whole condition is always true. That is why every sample ended up in the first bucket.

There is a second, subtler issue not shown in the replies: the generator is advanced twice per loop. The loop calls GenerateNext() and then DivideModulus() — but DivideModulus() itself calls GenerateNext() again. That makes the integer value used for the printed answer different from the value used to compute the normalized double. Ensure the generator is advanced exactly once per iteration and derive the normalized value from that same integer. A compact, safe bucket approach:

int value = r.GenerateNext();
double d = value / (double) r.getModulus();   // or compute from a known modulus field/getter
int bucket = (int)(d * 10.0);
if (bucket == 10) bucket = 9;
count[bucket]++;

Other practical improvements: perform the LCG arithmetic in a wider type (use long for intermediate multiplication) to avoid overflow, validate modulus > 0, and consider the Hull–Dobell conditions if a full period is required (increment relatively prime to modulus; multiplier−1 divisible by prime factors of modulus; extra check for modulus divisible by 4). As suggested, follow Java naming and packaging conventions and, for non-teaching/production code, prefer java.util.Random or SecureRandom depending on needs.

Recommended Answers

All 3 Replies

if (divmod >= 0.0 || divmod < 0.1)

One of the above two conditions is always true. You wanted to use the && symbol.

if (divmod >= 0.0 && divmod < 0.1)
commented: Very Helpful +2

Ok Made the changes and now it's working, Thanks!

now to change that code to actually match the coding standards.

  • class names should be in CamelCase with a starting capital
  • method names should be in camelCase without a starting capital
  • all classes should be inside a package
  • etc. etc. etc.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.