Java: Random Class & Simulating Particles in a Box

In summary, the conversation discusses the use of the nextGaussian() method to initialize particles with random velocities in a java simulation. There is concern about the way java stores variables, specifically in the Particle class, and whether it will store the address to r.nextGaussian() rather than just the value. Possible solutions include specifying a variable on the stack and using reference syntax in Java.
  • #1
Feldoh
1,342
3
I'm trying to program a java simulation of some particles in a box. Anyways I want to initialize the particles with random velocities such that on average they're distributed around some velocity.

To do this I was thinking about using the nextGaussian() method.

Anyways I was initializing my particles with something like (in a simplified version):

Code:
Random r = new Random();
double avg = 10;
Particle a = new Particle(xpos,ypos, r.nextGaussian()+avg,r.nextGaussian()+avg);

Where particle takes in 4 doubles: xpos,ypos,xvel,yvel.

Anyways I'm worried about the way java stores the variables. I'm worried that my particle class will store the addr to r.nextGaussian rather than just a number. This would be bad since then everytime I would call the xvel or yvel the number would change.

Is that assessment correct and does anyone know of a way to fix it if this is correct?
 
Technology news on Phys.org
  • #2
You didn't show the definition of the Particle class, but presumably it has four member variables of type double that the constructor sets. After a given Particle instance is constructed, the xvel and yvel values won't change unless some member function changes them.
 
  • #3
Feldoh said:
I'm trying to program a java simulation of some particles in a box. Anyways I want to initialize the particles with random velocities such that on average they're distributed around some velocity.

To do this I was thinking about using the nextGaussian() method.

Anyways I was initializing my particles with something like (in a simplified version):

Code:
Random r = new Random();
double avg = 10;
Particle a = new Particle(xpos,ypos, r.nextGaussian()+avg,r.nextGaussian()+avg);

Where particle takes in 4 doubles: xpos,ypos,xvel,yvel.

Anyways I'm worried about the way java stores the variables. I'm worried that my particle class will store the addr to r.nextGaussian rather than just a number. This would be bad since then everytime I would call the xvel or yvel the number would change.

Is that assessment correct and does anyone know of a way to fix it if this is correct?

You could specify a variable on the stack and just assign r.nextGaussian() to that variable.

Typically when you want to force a register or some word to take the value instead of the address the function has to pass the arguments BYVAL (by value) instead of BYREF (by reference).

In BASIC languages you use BYVAL or BYREF. In C/C++ you simply use the variable (eg int value) or you use the reference symbol (int &value) for passing the address. I'm not sure what you do in java but there's probably something like the syntax in C++ and if i recall correctly Java doesn't allow you to use pointers which lends me to think that there is definitely a reference syntax thing in there somewhere (I haven't used Java in over 5 years)
 
  • #4
Also I should mention that if the Particle class requires a reference then your code would not compile because you have not supplied a correct variable.

If the code compiles it should most likely be constructing the variable on the stack and then passing by value.
 
  • #5


I am familiar with using Java to simulate particles in a box. I can understand your concern about how Java stores variables and how it may affect the simulation.

Firstly, let me assure you that Java stores variables as values, not addresses. So in your code, the xvel and yvel values will be stored as numbers, not addresses.

However, there is a potential issue with your approach. Using the nextGaussian() method, you are generating random values that follow a normal distribution with a mean of 0 and a standard deviation of 1. By adding your desired average velocity (avg) to this value, you are essentially shifting the distribution of velocities. This may result in some particles having velocities that are significantly higher or lower than your desired average.

To ensure that your particles have velocities distributed around your desired average, you could use the nextDouble() method instead. This will generate random values between 0.0 and 1.0, which you can then scale to fit your desired velocity range. For example, if you want velocities between -10 and 10, you could do something like this:

Random r = new Random();
double avg = 10;
Particle a = new Particle(xpos, ypos, (r.nextDouble() * 20) - 10, (r.nextDouble() * 20) - 10);

This approach will generate velocities that are more evenly distributed around your desired average.

Additionally, you can also use the setSeed() method to set a specific seed for your random number generator. This will ensure that your simulation is reproducible and consistent each time it is run.

In conclusion, while your assessment of how Java stores variables is not entirely correct, there are some improvements that can be made to your approach to ensure a more accurate simulation. I hope this helps and good luck with your simulation!
 

1. What is the Java Random Class?

The Java Random Class is a class in the Java programming language that is used to generate random numbers and values. It is commonly used in simulations, games, and other applications that require randomization.

2. How do you use the Java Random Class?

To use the Java Random Class, you first need to import the class into your program. Then, you can create an instance of the class and use its methods to generate random numbers or values. You can also specify a seed value for the random number generator to ensure that the same sequence of numbers is generated each time the program is run.

3. How can the Java Random Class be used to simulate particles in a box?

The Java Random Class can be used to simulate particles in a box by generating random positions and velocities for the particles. These values can then be used in a simulation to determine the behavior of the particles within the box.

4. What are the advantages of using the Java Random Class for particle simulation?

One advantage of using the Java Random Class for particle simulation is that it allows for a more realistic and dynamic simulation. By generating random values for each particle, the simulation can account for unpredictable interactions and movements between particles.

5. Are there any limitations to using the Java Random Class for particle simulation?

Yes, there are some limitations to using the Java Random Class for particle simulation. One limitation is that the random numbers generated are not truly random, but rather pseudo-random. This means that the sequence of numbers generated can be predicted if the seed value is known. Additionally, the Java Random Class may not be suitable for more complex or large-scale simulations that require more precise randomization.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
4K
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top