Java question: Need some sort of global variable.

In summary, a global variable in Java is a variable that can be accessed and used by any part of a program. It is declared outside of any function or class using the keyword "static". The advantages of using global variables include easy data sharing and decreased need for passing variables between functions, but they can also make debugging more difficult and code less modular. It is generally not recommended to use global variables in Java, as it is considered better practice to use local variables and pass variables between functions when necessary.
  • #1
Jamin2112
986
12
Just wondering if the Java experts here could help me. As part of a project I'm making a function rand9() that returns a pseudo-random number 0 through 9. The problem is that each time it's called I need a certain number to increment so that it can generate a new (not necessarily different) random number next time it's called. Hope that makes sense. Just wondering how to approach this. I've checked out http://www.glenmccl.com/tip_002.htm, but I'm not sure how to how to implement that because, at least with JGrasp, I can only have 1 class in a file.
 
Last edited:
Technology news on Phys.org
  • #2
By the way, I just ran a simulation (using C++ since I suck at Java) and it looks like my random number distributor is pretty uniform.

TOTAL:
# of 0's: 226
# of 1's: 232
# of 2's: 208
# of 3's: 247
# of 4's: 230
# of 5's: 227
# of 6's: 240
# of 7's: 208
# of 8's: 226
# of 9's: 216


EDIT:

Dang it! There's a little repetition. I noticed that a 0 is appearing every 50th character eventually in the output. My formula for the nth number is f(n) = (n * ∏(n)) mod 10.

5, 8, 5, 8, 9, 2, 7, 0, 1, 4, 3, 2, 5, 8, 3, 8, 3, 0, 3, 8, 9, 6, 5, 6, 1, 6, 1, 0, 7, 2, 1, 6, 5, 6, 9, 4, 3, 0, 9, 2, 3, 2, 5, 4, 7, 4, 3, 0, 3, 8, 3, 4, 5, 8, 3, 8, 3, 0, 3, 6, 1, 4, 5, 2, 7, 6, 3, 0, 3, 8, 1, 2, 5, 8, 3, 6, 9, 0, 9, 2, 3, 2, 5, 8, 3, 6, 9, 0, 7, 8, 1, 4, 5, 8, 3, 8, 7, 0, 7, 4, 9, 6, 5, 2, 9, 4, 1, 0, 7, 6, 1, 6, 5, 6, 1, 6, 7, 0, 1, 6, 1, 2, 5, 6, 3, 2, 3, 0, 9, 6, 3, 8, 5, 4, 1, 6, 3, 0, 1, 2, 9, 8, 5, 4, 1, 6,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 1, 6, 9, 0, 7, 8, 9, 4, 5, 2, 1, 2, 9, 0, 3, 8, 3, 8, 5, 6, 1, 4, 1, 0, 7, 6, 1, 2, 5, 6, 7, 6, 3, 0, 9, 4, 1, 6, 5, 8, 3, 4, 3, 0, 1, 6, 7, 4, 5, 8, 7, 6, 1, 0, 3, 8, 1, 8, 5, 6, 1, 4, 1, 0, 3, 4,
 
Last edited:
  • #3
Jamin2112 said:
By the way, I just ran a simulation (using C++ since I suck at Java) and it looks like my random number distributor is pretty uniform.

Look at the header files of http://www.boost.org/doc/libs/1_53_0/doc/html/boost_random.html, pick your flavor and recode that with Java. (And pls don't forget to properly credit the inventors of the algorithms).

About the static var issue:
- If your program is single-threaded you already have all pieces,
- BUT don't make that var "public", but add it to your RNG class itself with "private" visibility.

Regards, Solkar
 
  • #4
Solkar said:
(And pls don't forget to properly credit the inventors of the algorithms).

I didn't know it was already invented.
 
  • #5
Unless the purpose of this is to try implementing an random number generator then I'd just use an off-the-shelf one. For Java something in http://commons.apache.org/proper/commons-math/ would probably work. Note that it also supports http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/random/SynchronizedRandomGenerator.html if you want to use it in a multi-threaded environment.

Javadoc of the random generator interface in that library:
http://commons.apache.org/proper/commons-math/javadocs/api-3.2/index.html

As to static variables in Java that example is fine. I am not sure what you mean about about only one class per file being a jGRASP thing (also why jGRASP instead of Eclipse, NetBeans, or even the free version of Intellij?). Having only one top level class per file is just part of the Java language. You just put the Global class in a separate file and then make sure its on the classpath when you build your main class.
 
  • #6
I might hide the random number generator in a static function (that returns the random number), and also you need to think about whether multiple threads will be calling the static function. If they are, the static function itself needs to complete before another thread can call it. There is a way to do that, but right off hand, I am not remembering it.
 
  • #7
harborsparrow said:
I might hide the random number generator in a static function (that returns the random number), and also you need to think about whether multiple threads will be calling the static function. If they are, the static function itself needs to complete before another thread can call it. There is a way to do that, but right off hand, I am not remembering it.
synchronized
 

Related to Java question: Need some sort of global variable.

1. What is a global variable in Java?

A global variable in Java is a variable that can be accessed and used by any part of a program, regardless of its scope. It is declared outside of any function or class and can be accessed by all functions and classes within the program.

2. How do you declare a global variable in Java?

To declare a global variable in Java, you would use the keyword "static" before the variable's data type when declaring it outside of any function or class. This will make the variable accessible to all functions and classes within the program.

3. What are the advantages of using global variables in Java?

Global variables in Java can be useful for storing and sharing data between different functions and classes within a program. They can also help reduce the need for passing variables between functions, making the code more streamlined and efficient.

4. What are the potential drawbacks of using global variables in Java?

Using global variables in Java can make it difficult to track and debug errors within a program, as any function or class can modify the value of the global variable. It can also make the code less modular and harder to maintain.

5. Are global variables recommended to use in Java?

It is generally not recommended to use global variables in Java unless absolutely necessary. It is considered a better practice to use local variables within functions and pass variables between functions when needed, as this can make the code more organized and easier to debug.

Similar threads

  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
18
Views
8K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top