Random Class in Java

  • Java
  • Thread starter Feldoh
  • Start date
  • #1
1,341
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?
 

Answers and Replies

  • #2
35,052
6,791
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
chiro
Science Advisor
4,790
132
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 theres 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
chiro
Science Advisor
4,790
132
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.
 

Related Threads on Random Class in Java

Replies
2
Views
3K
Replies
2
Views
3K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
8
Views
2K
Top