Java Java question: Need some sort of global variable.

AI Thread Summary
The discussion revolves around creating a Java function, rand9(), that generates pseudo-random numbers between 0 and 9, with a requirement for the function to increment a number each time it's called. The user expresses confusion about implementation, particularly regarding class structure in JGrasp, which allows only one class per file. They share simulation results from a C++ program indicating a uniform distribution of random numbers, but also note a pattern of repetition in the output.Several contributors suggest utilizing existing libraries, such as Apache Commons Math, for random number generation, which could simplify the task. They also discuss the importance of managing static variables in Java, especially in multi-threaded environments, emphasizing the need for proper synchronization to avoid issues when multiple threads access the random number generator. The conversation highlights the necessity of understanding Java's class structure and the potential benefits of using established libraries for random number generation.
Jamin2112
Messages
973
Reaction score
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
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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, 5, 8, 3, 2, 1, 0, 1, 6, 1, 6, 5, 4, 1, 2, 9, 0, 3, 2, 1, 4, 5, 8, 3, 8, 3, 0, 9, 2, 1, 2, 5, 8, 9, 8, 7, 0, 1, 4, 7, 6, 5, 8, 3, 8, 1, 0, 3, 6, 7, 4, 5, 6, 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:
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
 
Solkar said:
(And pls don't forget to properly credit the inventors of the algorithms).

I didn't know it was already invented.
 
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.
 
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.
 
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
 

Similar threads

Replies
18
Views
9K
Replies
4
Views
2K
Replies
4
Views
5K
Replies
3
Views
3K
Replies
1
Views
1K
Back
Top