Java Java: Random Class & Simulating Particles in a Box

AI Thread Summary
In a Java simulation of particles, the goal is to initialize particles with random velocities centered around a specific average using the nextGaussian() method. The concern arises regarding how Java handles variable storage, specifically whether the Particle class might store a reference to the random velocity values instead of the actual numbers. This could lead to unexpected changes in velocity values upon subsequent calls. However, the discussion clarifies that when a Particle instance is created, the xvel and yvel values are set as doubles, which means they will not change unless explicitly modified by a method. To ensure that the random values are stored correctly, it is suggested to assign the output of nextGaussian() to a temporary variable before passing it to the Particle constructor. This ensures that the values are passed by value, not by reference, thus maintaining their integrity within the Particle instance. The conversation also touches on Java's handling of references and values, indicating that if the code compiles, it is likely functioning as intended.
Feldoh
Messages
1,336
Reaction score
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
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.
 
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)
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top