Java: Random Class & Simulating Particles in a Box

  • Context: Java 
  • Thread starter Thread starter Feldoh
  • Start date Start date
  • Tags Tags
    Class Java Random
Click For Summary

Discussion Overview

The discussion revolves around programming a Java simulation of particles in a box, specifically focusing on initializing particles with random velocities using the nextGaussian() method from the Random class. Participants explore concerns about variable storage and the implications of reference versus value passing in Java.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses concern that the Particle class might store a reference to the result of r.nextGaussian(), which could lead to changing velocity values upon subsequent calls.
  • Another participant suggests that the Particle class likely has member variables of type double, which would not change after construction unless explicitly modified by a member function.
  • A participant proposes using a local variable to store the result of r.nextGaussian() before passing it to the Particle constructor to ensure a fixed value is used.
  • There is a mention of the concept of passing by value versus passing by reference, with references to how this is handled in other programming languages like C/C++ and BASIC.
  • Another participant notes that if the Particle class requires a reference, the code would not compile, implying that the values are likely being passed by value if the code compiles successfully.

Areas of Agreement / Disagreement

Participants have differing views on the implications of variable storage and the mechanics of Java's handling of references and values. There is no consensus on the best approach to ensure the velocities remain constant after initialization.

Contextual Notes

Participants reference concepts from other programming languages, which may not directly apply to Java. There is uncertainty regarding the specifics of Java's memory management and variable passing mechanisms.

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.
 

Similar threads

Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
8
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K